Skip to content

Commit b8ab533

Browse files
GeoffreyBoothtargos
authored andcommitted
module: remove --experimental-default-type
PR-URL: #56092 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 30d6041 commit b8ab533

29 files changed

+86
-869
lines changed

doc/api/cli.md

+6-43
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ For more info about `node inspect`, see the [debugger][] documentation.
2525

2626
The program entry point is a specifier-like string. If the string is not an
2727
absolute path, it's resolved as a relative path from the current working
28-
directory. That path is then resolved by [CommonJS][] module loader, or by the
29-
[ES module loader][Modules loaders] if [`--experimental-default-type=module`][]
30-
is passed. If no corresponding file is found, an error is thrown.
28+
directory. That path is then resolved by [CommonJS][] module loader. If no
29+
corresponding file is found, an error is thrown.
3130

3231
If a file is found, its path will be passed to the
3332
[ES module loader][Modules loaders] under any of the following conditions:
3433

3534
* The program was started with a command-line flag that forces the entry
36-
point to be loaded with ECMAScript module loader, such as `--import` or
37-
[`--experimental-default-type=module`][].
35+
point to be loaded with ECMAScript module loader, such as `--import`.
3836
* The file has an `.mjs` extension.
3937
* The file does not have a `.cjs` extension, and the nearest parent
4038
`package.json` file contains a top-level [`"type"`][] field with a value of
@@ -47,9 +45,8 @@ Otherwise, the file is loaded using the CommonJS module loader. See
4745

4846
When loading, the [ES module loader][Modules loaders] loads the program
4947
entry point, the `node` command will accept as input only files with `.js`,
50-
`.mjs`, or `.cjs` extensions; with `.wasm` extensions when
51-
[`--experimental-wasm-modules`][] is enabled; and with no extension when
52-
[`--experimental-default-type=module`][] is passed.
48+
`.mjs`, or `.cjs` extensions; and with `.wasm` extensions when
49+
[`--experimental-wasm-modules`][] is enabled.
5350

5451
## Options
5552

@@ -910,38 +907,6 @@ flows within the application. As such, it is presently recommended to be sure
910907
your application behaviour is unaffected by this change before using it in
911908
production.
912909

913-
### `--experimental-default-type=type`
914-
915-
<!-- YAML
916-
added:
917-
- v21.0.0
918-
- v20.10.0
919-
- v18.19.0
920-
-->
921-
922-
> Stability: 1.0 - Early development
923-
924-
Define which module system, `module` or `commonjs`, to use for the following:
925-
926-
* String input provided via `--eval` or STDIN, if `--input-type` is unspecified.
927-
928-
* Files ending in `.js` or with no extension, if there is no `package.json` file
929-
present in the same folder or any parent folder.
930-
931-
* Files ending in `.js` or with no extension, if the nearest parent
932-
`package.json` field lacks a `"type"` field; unless the `package.json` folder
933-
or any parent folder is inside a `node_modules` folder.
934-
935-
In other words, `--experimental-default-type=module` flips all the places where
936-
Node.js currently defaults to CommonJS to instead default to ECMAScript modules,
937-
with the exception of folders and subfolders below `node_modules`, for backward
938-
compatibility.
939-
940-
Under `--experimental-default-type=module` and `--experimental-wasm-modules`,
941-
files with no extension will be treated as WebAssembly if they begin with the
942-
WebAssembly magic number (`\0asm`); otherwise they will be treated as ES module
943-
JavaScript.
944-
945910
### `--experimental-transform-types`
946911

947912
<!-- YAML
@@ -1418,7 +1383,7 @@ added: v12.0.0
14181383

14191384
This configures Node.js to interpret `--eval` or `STDIN` input as CommonJS or
14201385
as an ES module. Valid values are `"commonjs"` or `"module"`. The default is
1421-
`"commonjs"` unless [`--experimental-default-type=module`][] is used.
1386+
`"commonjs"`.
14221387

14231388
The REPL does not support this option. Usage of `--input-type=module` with
14241389
[`--print`][] will throw an error, as `--print` does not support ES module
@@ -3059,7 +3024,6 @@ one is included in the list below.
30593024
* `--entry-url`
30603025
* `--experimental-abortcontroller`
30613026
* `--experimental-async-context-frame`
3062-
* `--experimental-default-type`
30633027
* `--experimental-detect-module`
30643028
* `--experimental-eventsource`
30653029
* `--experimental-import-meta-resolve`
@@ -3621,7 +3585,6 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
36213585
[`--diagnostic-dir`]: #--diagnostic-dirdirectory
36223586
[`--env-file-if-exists`]: #--env-file-if-existsconfig
36233587
[`--env-file`]: #--env-fileconfig
3624-
[`--experimental-default-type=module`]: #--experimental-default-typetype
36253588
[`--experimental-sea-config`]: single-executable-applications.md#generating-single-executable-preparation-blobs
36263589
[`--experimental-strip-types`]: #--experimental-strip-types
36273590
[`--experimental-wasm-modules`]: #--experimental-wasm-modules

doc/api/esm.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ Node.js has two module systems: [CommonJS][] modules and ECMAScript modules.
122122

123123
Authors can tell Node.js to interpret JavaScript as an ES module via the `.mjs`
124124
file extension, the `package.json` [`"type"`][] field with a value `"module"`,
125-
the [`--input-type`][] flag with a value of `"module"`, or the
126-
[`--experimental-default-type`][] flag with a value of `"module"`. These are
127-
explicit markers of code being intended to run as an ES module.
125+
or the [`--input-type`][] flag with a value of `"module"`. These are explicit
126+
markers of code being intended to run as an ES module.
128127

129-
Inversely, authors can tell Node.js to interpret JavaScript as CommonJS via the
130-
`.cjs` file extension, the `package.json` [`"type"`][] field with a value
131-
`"commonjs"`, the [`--input-type`][] flag with a value of `"commonjs"`, or the
132-
[`--experimental-default-type`][] flag with a value of `"commonjs"`.
128+
Inversely, authors can explicitly tell Node.js to interpret JavaScript as
129+
CommonJS via the `.cjs` file extension, the `package.json` [`"type"`][] field
130+
with a value `"commonjs"`, or the [`--input-type`][] flag with a value of
131+
`"commonjs"`.
133132

134133
When code lacks explicit markers for either module system, Node.js will inspect
135134
the source code of a module to look for ES module syntax. If such syntax is
@@ -1120,7 +1119,6 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
11201119
[URL]: https://url.spec.whatwg.org/
11211120
[`"exports"`]: packages.md#exports
11221121
[`"type"`]: packages.md#type
1123-
[`--experimental-default-type`]: cli.md#--experimental-default-typetype
11241122
[`--input-type`]: cli.md#--input-typetype
11251123
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
11261124
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

doc/api/module.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,7 @@ Omitting vs providing a `source` for `'commonjs'` has very different effects:
980980
registered hooks. This behavior for nullish `source` is temporary — in the
981981
future, nullish `source` will not be supported.
982982
983-
When `node` is run with `--experimental-default-type=commonjs`, the Node.js
984-
internal `load` implementation, which is the value of `next` for the
983+
The Node.js internal `load` implementation, which is the value of `next` for the
985984
last hook in the `load` chain, returns `null` for `source` when `format` is
986985
`'commonjs'` for backward compatibility. Here is an example hook that would
987986
opt-in to using the non-default behavior:

doc/api/packages.md

+20-33
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ expressions:
7373
`import` or `export` statements or `import.meta`, with no explicit marker of
7474
how it should be interpreted. Explicit markers are `.mjs` or `.cjs`
7575
extensions, `package.json` `"type"` fields with either `"module"` or
76-
`"commonjs"` values, or `--input-type` or `--experimental-default-type` flags.
77-
Dynamic `import()` expressions are supported in either CommonJS or ES modules
78-
and would not force a file to be treated as an ES module. See
79-
[Syntax detection][].
76+
`"commonjs"` values, or the `--input-type` flag. Dynamic `import()`
77+
expressions are supported in either CommonJS or ES modules and would not force
78+
a file to be treated as an ES module. See [Syntax detection][].
8079

8180
Node.js will treat the following as [CommonJS][] when passed to `node` as the
8281
initial input, or when referenced by `import` statements or `import()`
@@ -90,30 +89,21 @@ expressions:
9089
* Strings passed in as an argument to `--eval` or `--print`, or piped to `node`
9190
via `STDIN`, with the flag `--input-type=commonjs`.
9291

93-
Aside from these explicit cases, there are other cases where Node.js defaults to
94-
one module system or the other based on the value of the
95-
[`--experimental-default-type`][] flag:
96-
97-
* Files ending in `.js` or with no extension, if there is no `package.json` file
98-
present in the same folder or any parent folder.
99-
100-
* Files ending in `.js` or with no extension, if the nearest parent
101-
`package.json` field lacks a `"type"` field; unless the folder is inside a
102-
`node_modules` folder. (Package scopes under `node_modules` are always treated
103-
as CommonJS when the `package.json` file lacks a `"type"` field, regardless
104-
of `--experimental-default-type`, for backward compatibility.)
105-
106-
* Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`,
107-
when `--input-type` is unspecified.
108-
109-
This flag currently defaults to `"commonjs"`, but it may change in the future to
110-
default to `"module"`. For this reason it is best to be explicit wherever
111-
possible; in particular, package authors should always include the [`"type"`][]
112-
field in their `package.json` files, even in packages where all sources are
113-
CommonJS. Being explicit about the `type` of the package will future-proof the
114-
package in case the default type of Node.js ever changes, and it will also make
115-
things easier for build tools and loaders to determine how the files in the
116-
package should be interpreted.
92+
* Files with a `.js` extension with no parent `package.json` file or where the
93+
nearest parent `package.json` file lacks a `type` field, and where the code
94+
can evaluate successfully as CommonJS. In other words, Node.js tries to run
95+
such "ambiguous" files as CommonJS first, and will retry evaluating them as ES
96+
modules if the evaluation as CommonJS fails because the parser found ES module
97+
syntax.
98+
99+
Writing ES module syntax in "ambiguous" files incurs a performance cost, and
100+
therefore it is encouraged that authors be explicit wherever possible. In
101+
particular, package authors should always include the [`"type"`][] field in
102+
their `package.json` files, even in packages where all sources are CommonJS.
103+
Being explicit about the `type` of the package will future-proof the package in
104+
case the default type of Node.js ever changes, and it will also make things
105+
easier for build tools and loaders to determine how the files in the package
106+
should be interpreted.
117107

118108
### Syntax detection
119109

@@ -137,10 +127,8 @@ as an ES module.
137127
Ambiguous input is defined as:
138128

139129
* Files with a `.js` extension or no extension; and either no controlling
140-
`package.json` file or one that lacks a `type` field; and
141-
`--experimental-default-type` is not specified.
142-
* String input (`--eval` or STDIN) when neither `--input-type` nor
143-
`--experimental-default-type` are specified.
130+
`package.json` file or one that lacks a `type` field.
131+
* String input (`--eval` or `STDIN`) when `--input-type`is not specified.
144132

145133
ES module syntax is defined as syntax that would throw when evaluated as
146134
CommonJS. This includes the following:
@@ -1162,7 +1150,6 @@ This field defines [subpath imports][] for the current package.
11621150
[`"packageManager"`]: #packagemanager
11631151
[`"type"`]: #type
11641152
[`--conditions` / `-C` flag]: #resolving-user-conditions
1165-
[`--experimental-default-type`]: cli.md#--experimental-default-typetype
11661153
[`--no-addons` flag]: cli.md#--no-addons
11671154
[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported
11681155
[`package.json`]: #nodejs-packagejson-field-definitions

doc/node.1

-5
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ Enable Source Map V3 support for stack traces.
163163
.It Fl -entry-url
164164
Interpret the entry point as a URL.
165165
.
166-
.It Fl -experimental-default-type Ns = Ns Ar type
167-
Interpret as either ES modules or CommonJS modules input via --eval or STDIN, when --input-type is unspecified;
168-
.js or extensionless files with no sibling or parent package.json;
169-
.js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules.
170-
.
171166
.It Fl -experimental-import-meta-resolve
172167
Enable experimental ES modules support for import.meta.resolve().
173168
.

lib/internal/main/check_syntax.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ function loadESMIfNeeded(cb) {
5959
async function checkSyntax(source, filename) {
6060
let format;
6161
if (filename === '[stdin]' || filename === '[eval]') {
62-
format = (getOptionValue('--input-type') === 'module' ||
63-
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) ?
64-
'module' : 'commonjs';
62+
format = (getOptionValue('--input-type') === 'module') ? 'module' : 'commonjs';
6563
} else {
6664
const { defaultResolve } = require('internal/modules/esm/resolve');
6765
const { defaultGetFormat } = require('internal/modules/esm/get_format');

lib/internal/main/eval_stdin.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ readStdin((code) => {
2525

2626
const print = getOptionValue('--print');
2727
const shouldLoadESM = getOptionValue('--import').length > 0;
28-
if (getOptionValue('--input-type') === 'module' ||
29-
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
28+
if (getOptionValue('--input-type') === 'module') {
3029
evalModuleEntryPoint(code, print);
3130
} else {
3231
evalScript('[stdin]',

lib/internal/main/eval_string.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ const source = getOptionValue('--experimental-strip-types') ?
2929

3030
const print = getOptionValue('--print');
3131
const shouldLoadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0;
32-
if (getOptionValue('--input-type') === 'module' ||
33-
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
32+
if (getOptionValue('--input-type') === 'module') {
3433
evalModuleEntryPoint(source, print);
3534
} else {
3635
// For backward compatibility, we want the identifier crypto to be the

lib/internal/main/run_main_module.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ if (isEntryURL) {
2323
emitExperimentalWarning('--entry-url');
2424
}
2525

26-
if (getOptionValue('--experimental-default-type') === 'module') {
27-
require('internal/modules/run_main').executeUserEntryPoint(mainEntry);
28-
} else {
29-
/**
30-
* To support legacy monkey-patching of `Module.runMain`, we call `runMain` here to have the CommonJS loader begin
31-
* the execution of the main entry point, even if the ESM loader immediately takes over because the main entry is an
32-
* ES module or one of the other opt-in conditions (such as the use of `--import`) are met. Users can monkey-patch
33-
* before the main entry point is loaded by doing so via scripts loaded through `--require`. This monkey-patchability
34-
* is undesirable and is removed in `--experimental-default-type=module` mode.
35-
*/
36-
require('internal/modules/cjs/loader').Module.runMain(mainEntry);
37-
}
26+
/**
27+
* To support legacy monkey-patching of `Module.runMain`, we call `runMain` here to have the CommonJS loader begin the
28+
* execution of the main entry point, even if the ESM loader immediately takes over because the main entry is an ES
29+
* module or one of the other opt-in conditions (such as the use of `--import`) are met. Users can monkey-patch before
30+
* the main entry point is loaded by doing so via scripts loaded through `--require`. This monkey-patchability is
31+
* undesirable and should be removed once the module customization hooks provide equivalent functionality.
32+
*/
33+
require('internal/modules/cjs/loader').Module.runMain(mainEntry);

lib/internal/modules/esm/formats.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function mimeToFormat(mime) {
4646
}
4747

4848
/**
49-
* For extensionless files in a `module` package scope, or a default `module` scope enabled by the
50-
* `--experimental-default-type` flag, we check the file contents to disambiguate between ES module JavaScript and Wasm.
49+
* For extensionless files in a `module` package scope, we check the file contents to disambiguate between ES module
50+
* JavaScript and Wasm.
5151
* We do this by taking advantage of the fact that all Wasm files start with the header `0x00 0x61 0x73 0x6d` (`_asm`).
5252
* @param {URL} url
5353
*/

0 commit comments

Comments
 (0)