-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
esm: --experimental-default-type flag to flip module defaults
#49869
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
esm: --experimental-default-type flag to flip module defaults
#49869
Conversation
|
Review requested:
|
guybedford
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @GeoffreyBooth for continuing to push this work, this seems like a step in the right direction to me.
My only suggestion would be considering an alternative flag name like --default-type as an experimental flag or --experimental-default-type so that it is clear from the start that it is not an authoritative flag.
e6069f0 to
fbe28e2
Compare
This comment was marked as resolved.
This comment was marked as resolved.
bf5c677 to
6f0e209
Compare
guybedford
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking for approval conditional on resolving the naming discussion.
1cb1578 to
62b4b09
Compare
This PR implements most of the proposal in #49432. It creates a new flag,
--experimental-default-type, which supports valuescommonjs(the default) ormodule. Under--experimental-default-type=module, Node.js interprets the following as ES modules:String input provided via
--evalor STDIN, if--input-typeis unspecified.Files ending in
.jsor with no extension, if there is nopackage.jsonfile present in the same folder or any parent folder.Files ending in
.jsor with no extension, if the nearest parentpackage.jsonfield lacks atypefield; unless the folder is inside anode_modulesfolder.Extensionless files are interpreted as Wasm if
--experimental-wasm-modulesis passed and the file contains the “magic bytes” Wasm header.The ESM loader is used for all entry points, and we don’t pass through
Module.runMain. Under this flag, we no longer support monkey-patching of that method by files loaded via--require.This PR does not implement the “parse the entry point as a URL” part of #49432, as there are still some design decisions to be made around that and it’s substantial enough to warrant being implemented as a separate PR.
This is related to #49629 and linked PRs, which include the above behaviors for extensionless files but don’t require a flag. I think we can potentially ship that change on its own, separate from making this flag the new default behavior, because extensionless files currently error in
modulescopes. I’d prefer to ship it behind the--experimental-default-typeflag first to work out any kinks before splitting it out to not require any flag.If people don’t mind, I’d like if this PR thread can focus on the implementation. If anyone has any thoughts on the design or intentions behind this, there’s already a detailed thread for that at #49432.
cc @nodejs/loaders @nodejs/wasi @nodejs/startup @nodejs/tsc
Notable change
The new flag
--experimental-default-typecan be used to flip the default module system used by Node.js. Input that is already explicitly defined as ES modules or CommonJS, such as by apackage.json"type"field or.mjs/.cjsfile extension or the--input-typeflag, is unaffected. What is currently implicitly CommonJS would instead be interpreted as ES modules under--experimental-default-type=module:String input provided via
--evalor STDIN, if--input-typeis unspecified.Files ending in
.jsor with no extension, if there is nopackage.jsonfile present in the same folder or any parent folder.Files ending in
.jsor with no extension, if the nearest parentpackage.jsonfield lacks atypefield; unless the folder is inside anode_modulesfolder.In addition, extensionless files are interpreted as Wasm if
--experimental-wasm-modulesis passed and the file contains the “magic bytes” Wasm header.