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

Node can't recognize static import of ES module when theres's not explicit marker #51057

Closed
jasonjiicloud opened this issue Dec 5, 2023 · 9 comments · Fixed by #51089
Closed
Labels
good first issue Issues that are suitable for first-time contributors.

Comments

@jasonjiicloud
Copy link

Version

v20.10.0

Platform

Darwin a.local 21.6.0 Darwin Kernel Version 21.6.0: Fri Sep 15 16:17:23 PDT 2023; root:xnu-8020.240.18.703.5~1/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

A "main.js" file with code import path from 'path';, nothing more.

run node main.js

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior? Why is that the expected behavior?

According to the document https://nodejs.org/dist/latest-v20.x/docs/api/packages.html,

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.

@meyfa
Copy link
Contributor

meyfa commented Dec 5, 2023

The docs seem incomplete. Detection is an experimental feature and currently requires the flag --experimental-detect-module. Ref: #50096

@jasonjiicloud
Copy link
Author

Yes, @meyfa , I tried --experimental-detect-module, it worked as expected. The documents don't mention this flag.

@aduh95 aduh95 added the good first issue Issues that are suitable for first-time contributors. label Dec 5, 2023
@jangala-laxman
Copy link

@jasonjiicloud, did you add this "type":"module" in your package.json file. if not, this is causing the issue.

@aduh95
Copy link
Contributor

aduh95 commented Dec 5, 2023

The issue is in the documentation, as said above.

@DylanTet
Copy link
Contributor

DylanTet commented Dec 5, 2023

@aduh95 id like to handle this, do the docs just need to be updated to mention using the flag?

@shubherthi-mitra
Copy link
Contributor

shubherthi-mitra commented 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 command node --experimental-modules main.js @aduh95 let me know if I can help any further to resolve the issue?

@aduh95
Copy link
Contributor

aduh95 commented Dec 7, 2023

let me know if I can help any further to resolve the issue?

Yes, you could open a PR to fix the docs.

@shubherthi-mitra
Copy link
Contributor

shubherthi-mitra commented Dec 7, 2023

let me know if I can help any further to resolve the issue?

Yes, you could open a PR to fix the docs.

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-module is 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
Copy link
Contributor

@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.

aduh95 pushed a commit that referenced this issue Dec 9, 2023
PR-URL: #51089
Fixes: #51057
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
RafaelGSS pushed a commit that referenced this issue Dec 15, 2023
PR-URL: #51089
Fixes: #51057
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
richardlau pushed a commit that referenced this issue Mar 25, 2024
PR-URL: #51089
Fixes: #51057
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants