Skip to content

Conversation

@anthumchris
Copy link
Contributor

Specify import alongside require() syntax

@anthumchris anthumchris requested review from a team and leobalter as code owners January 4, 2026 15:51
- A folder with a `package.json` file containing a `"main"` field.
- A JavaScript file.

To use the `import` syntax, a module also must also include `"type": "module"` in its `package.json` file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? I thought it was optional, but the compromise was in performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excluding "type: module" shows a verbose warning:

$ node -v
v25.2.1

$ ls
package-cjs.json    package-esm.json    test.js

$ cat test.js 
import path from 'node:path'
console.log('test:', typeof path)

$ ln -fs package-cjs.json package.json && cat $_ && node test.js
{
  "name": "module-test"
}
(node:2825) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///Users/kratos/tmp/module-test/test.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /Users/kratos/tmp/module-test/package.json.
(Use `node --trace-warnings ...` to show where the warning was created)
test: object

$ ln -fs package-esm.json package.json && cat $_ && node test.js
{
  "name": "module-test",
  "type": "module"
}
test: object

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep I remembered correctly.

This incurs a performance overhead.

This can read "should" instead of "must" imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 8b0ec24

"type: module" is not requiret, but recommended to avoide a perfomance impact and a verbose warning message
@wraithgar wraithgar merged commit 22f731c into npm:main Jan 5, 2026
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.

2 participants