Skip to content

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

@jasonjiicloud

Description

@jasonjiicloud

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions