You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Node can't recognize static import of ES module when theres's not explicit marker #51057
In following case, code will be treated as ES module:
Code that contains syntax that only parses successfully as ES modules, such as import or export statements or import.meta, when the code has no explicit marker of how it should be interpreted.
What do you see instead?
Error displayed:
(node:10393) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
/Users/jasonji/main.js:1
import path from 'path';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Additional information
The behavior of Node.js is not consistent with the document.
@jasonjiicloud you can add "type":"module" (by default if the "type" is omitted the "type" is "commonjs") in your package.json file or you can run file with the command node --experimental-modules main.js@aduh95 let me know if I can help any further to resolve the issue?
@aduh95 I made a pull request. It's showing 5 workflows awaiting approval and Merging is blocked. Did I do anything wrong? I'm fairly new to github. Please help.
Activity
meyfa commentedon Dec 5, 2023
The docs seem incomplete. Detection is an experimental feature and currently requires the flag
--experimental-detect-module. Ref: #50096jasonjiicloud commentedon Dec 5, 2023
Yes, @meyfa , I tried
--experimental-detect-module, it worked as expected. The documents don't mention this flag.jangala-laxman commentedon Dec 5, 2023
@jasonjiicloud, did you add this "type":"module" in your package.json file. if not, this is causing the issue.
aduh95 commentedon Dec 5, 2023
The issue is in the documentation, as said above.
DylanTet commentedon Dec 5, 2023
@aduh95 id like to handle this, do the docs just need to be updated to mention using the flag?
shubherthi-mitra commentedon Dec 7, 2023
@jasonjiicloud you can add
"type":"module"(by default if the "type" is omitted the "type" is "commonjs") in your package.json file or you can run file with the commandnode --experimental-modules main.js@aduh95 let me know if I can help any further to resolve the issue?aduh95 commentedon Dec 7, 2023
Yes, you could open a PR to fix the docs.
shubherthi-mitra commentedon Dec 7, 2023
I checked https://github.com/nodejs/node/blob/main/doc/api/cli.md?plain=1 at line 676 and it's mentioned that
--experimental-detect-moduleis added to node versions v21.1.0 and v20.10.0 and is in early development. However, this (https://nodejs.org/dist/latest-v20.x/docs/api/packages.html), documentation is not updated as of now. I'd open a PR to fix packeges.md and add --experimental-detect-module flag.shubherthi-mitra commentedon Dec 7, 2023
@aduh95 I made a pull request. It's showing 5 workflows awaiting approval and Merging is blocked. Did I do anything wrong? I'm fairly new to github. Please help.
doc: add note regarding `--experimental-detect-module`
doc: add note regarding `--experimental-detect-module`
doc: add note regarding `--experimental-detect-module`