-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Closed
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
Description
Building off of #50043 (comment), this is an alternate proposal to #50043. This aims to achieve the same goals of allowing ESM syntax in “loose” files, where there are no package.json files present; and in files whose nearest parent package.json lacks a type field. This would permit ESM syntax without needing to opt in, while avoiding breaking existing scripts and tutorials. Like #50043, this proposal would avoid any breaking changes, and the aim is to eventually make this enabled by default without requiring a flag.
The new behavior would be as follows:
- Node would attempt to evaluate every ambiguous file (a file with no explicit
.mjsor.cjsextension, either nopackage.jsonor one that lacks atypefield) as CommonJS, as it already does today. If a file throws aSyntaxErrorfor something that is only allowed in ES modules (importorexportstatement,import.meta, top-levelawait), Node would try again to evaluate the file as an ES module. - This behavior would apply to ambiguous files within
node_modules. - This would apply to the main entry point and to files referenced via
import. It would not apply to files referenced viarequire. - This behavior would be disabled by any marker of explicitness:
.mjsor.cjsextension, apackage.jsontypefield,--input-typeor--experimental-default-type.
Pros, in comparison with #50043:
- Avoids the application behaving differently based on entry point: you can reference the entry point from a wrapper, such as a CommonJS test runner, and it will still run as ESM.
- Prevents the “package author published ambiguous ESM” problem. If a package author develops locally in this mode, relying on it to enable ESM for ambiguous files, their package would still work for consumers.
- This “per file” approach is the same as that taken by many other tools.
Cons:
- Presumably this is more of a performance impact, as we’re parsing every ambiguous ES module twice. We should probably measure this to see how much of an impact it is. There should be no impact for CommonJS files, and also no impact for ES modules in an explicit
package.jsonmodulescope. - Any files with ambiguous syntax, that could evaluate successfully as either CommonJS or ESM, would be run as CommonJS in this mode. A common example of such a file would be a polyfill that only sets a global variable, without importing or exporting anything.
@nodejs/loaders @nodejs/tsc
ovflowd, nickserv, danielbayley and ExE-Bosskarlhorky
Metadata
Metadata
Assignees
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.