Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import from ES Module #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

fix import from ES Module #92

wants to merge 1 commit into from

Conversation

il3ven
Copy link

@il3ven il3ven commented Jul 28, 2022

The problem was that Node was treating files in dist/esm as CommonJS. The solution would be to rename all files in dist/esm to .mjs or add a package.json with type: module. This PR chooses the second option.

Fixes #80

The problem was that Node was treating files in dist/esm as CommonJS.

The solution would be rename them to .mjs or add a package.json
with `type: module`.

This commit chooses the second option.
@sudiptosarkar
Copy link

This does not work. I tried it and it has problems with just-performance:

file:///home/xtreme/Source/private/JsAM/node_modules/limiter/dist/esm/clock.js:1
import { performance } from "just-performance";
         ^^^^^^^^^^^
SyntaxError: Named export 'performance' not found. The requested module 'just-performance' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'just-performance';
const { performance } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.9.0

@sudiptosarkar
Copy link

This does not work. I tried it and it has problems with just-performance:

file:///home/xtreme/Source/private/JsAM/node_modules/limiter/dist/esm/clock.js:1
import { performance } from "just-performance";
         ^^^^^^^^^^^
SyntaxError: Named export 'performance' not found. The requested module 'just-performance' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'just-performance';
const { performance } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.9.0

Also, I tried to import it like it says in the error here in clock.js and then it can't figure out export:

/home/xtreme/Source/private/JsAcMoF/node_modules/just-performance/dist/esm/node.js:1
export { performance } from "perf_hooks";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1027:15)
    at Module._compile (node:internal/modules/cjs/loader:1063:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)

@il3ven
Copy link
Author

il3ven commented Aug 2, 2022

This does not work. I tried it and it has problems with just-performance:

Yes, it won't work right now. A similar fix needs to be done for just-performance too. To test it locally, go to node_modules/just-performance/dist/esm and add a package.json. Similar to what this PR adds for node-rate-limiter.

package.json

{
  "type": "module"
}

@sudiptosarkar
Copy link

sudiptosarkar commented Aug 2, 2022

This does not work. I tried it and it has problems with just-performance:

Yes, it won't work right now. A similar fix needs to be done for just-performance too. To test it locally, go to node_modules/just-performance/dist/esm and add a package.json. Similar to what this PR adds for node-rate-limiter.

package.json

{
  "type": "module"
}

I wish I had seen your comment before now. It would have saved me some time.. I figured out the same thing you suggested and it worked. 🥲
I raised the corresponding PR: just-performance#4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Importing from esm in Node.js is broken in v2.1.0
2 participants