Skip to content

Commit 94c327c

Browse files
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 47b80c2 commit 94c327c

29 files changed

+86
-869
lines changed

doc/api/cli.md

Lines changed: 6 additions & 43 deletions
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

@@ -896,38 +893,6 @@ and `"` are usable.
896893
It is possible to run code containing inline types by passing
897894
[`--experimental-strip-types`][].
898895

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

933898
<!-- YAML
@@ -1404,7 +1369,7 @@ added: v12.0.0
14041369

14051370
This configures Node.js to interpret `--eval` or `STDIN` input as CommonJS or
14061371
as an ES module. Valid values are `"commonjs"` or `"module"`. The default is
1407-
`"commonjs"` unless [`--experimental-default-type=module`][] is used.
1372+
`"commonjs"`.
14081373

14091374
The REPL does not support this option. Usage of `--input-type=module` with
14101375
[`--print`][] will throw an error, as `--print` does not support ES module
@@ -3057,7 +3022,6 @@ one is included in the list below.
30573022
* `--enable-source-maps`
30583023
* `--entry-url`
30593024
* `--experimental-abortcontroller`
3060-
* `--experimental-default-type`
30613025
* `--experimental-detect-module`
30623026
* `--experimental-eventsource`
30633027
* `--experimental-import-meta-resolve`
@@ -3620,7 +3584,6 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
36203584
[`--diagnostic-dir`]: #--diagnostic-dirdirectory
36213585
[`--env-file-if-exists`]: #--env-file-if-existsconfig
36223586
[`--env-file`]: #--env-fileconfig
3623-
[`--experimental-default-type=module`]: #--experimental-default-typetype
36243587
[`--experimental-sea-config`]: single-executable-applications.md#generating-single-executable-preparation-blobs
36253588
[`--experimental-strip-types`]: #--experimental-strip-types
36263589
[`--experimental-wasm-modules`]: #--experimental-wasm-modules

doc/api/esm.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,13 @@ Node.js has two module systems: [CommonJS][] modules and ECMAScript modules.
124124

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

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

136135
When code lacks explicit markers for either module system, Node.js will inspect
137136
the source code of a module to look for ES module syntax. If such syntax is
@@ -1124,7 +1123,6 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
11241123
[URL]: https://url.spec.whatwg.org/
11251124
[`"exports"`]: packages.md#exports
11261125
[`"type"`]: packages.md#type
1127-
[`--experimental-default-type`]: cli.md#--experimental-default-typetype
11281126
[`--input-type`]: cli.md#--input-typetype
11291127
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
11301128
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

doc/api/module.md

Lines changed: 1 addition & 2 deletions
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

Lines changed: 20 additions & 33 deletions
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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 8 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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)