@@ -69,14 +69,14 @@ expressions:
69
69
* Strings passed in as an argument to ` --eval ` , or piped to ` node ` via ` STDIN ` ,
70
70
with the flag ` --input-type=module ` .
71
71
72
- * When using [ ` --experimental-detect-module ` ] [ ] , code containing syntax only
73
- successfully parsed as [ ES modules ] [ ] , such as ` import ` or ` export `
74
- statements or ` import.meta ` , having no explicit marker of how it should be
75
- interpreted. Explicit markers are ` .mjs ` or ` .cjs ` extensions, ` package.json `
76
- ` "type " ` fields with either ` "module" ` or ` "commonjs" ` values, or
77
- ` --input-type ` or ` --experimental-default-type ` flags. Dynamic ` import() `
78
- expressions are supported in either CommonJS or ES modules and would not
79
- cause a file to be treated as an ES module .
72
+ * Code containing syntax only successfully parsed as [ ES modules ] [ ] , such as
73
+ ` import ` or ` export ` statements or ` import.meta ` , with no explicit marker of
74
+ how it should be interpreted. Explicit markers are ` .mjs ` or ` .cjs `
75
+ 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 ] [ ] .
80
80
81
81
Node.js will treat the following as [ CommonJS] [ ] when passed to ` node ` as the
82
82
initial input, or when referenced by ` import ` statements or ` import() `
@@ -115,6 +115,44 @@ package in case the default type of Node.js ever changes, and it will also make
115
115
things easier for build tools and loaders to determine how the files in the
116
116
package should be interpreted.
117
117
118
+ ### Syntax detection
119
+
120
+ <!-- YAML
121
+ added:
122
+ - v21.1.0
123
+ - v20.10.0
124
+ changes:
125
+ - version:
126
+ - REPLACEME
127
+ pr-url: https://github.com/nodejs/node/pull/53619
128
+ description: Syntax detection is enabled by default.
129
+ -->
130
+
131
+ > Stability: 1.2 - Release candidate
132
+
133
+ Node.js will inspect the source code of ambiguous input to determine whether it
134
+ contains ES module syntax; if such syntax is detected, the input will be treated
135
+ as an ES module.
136
+
137
+ Ambiguous input is defined as:
138
+
139
+ * 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.
144
+
145
+ ES module syntax is defined as syntax that would throw when evaluated as
146
+ CommonJS. This includes the following:
147
+
148
+ * ` import ` statements (but _ not_ ` import() ` expressions, which are valid in
149
+ CommonJS).
150
+ * ` export ` statements.
151
+ * ` import.meta ` references.
152
+ * ` await ` at the top level of a module.
153
+ * Lexical redeclarations of the CommonJS wrapper variables (` require ` , ` module ` ,
154
+ ` exports ` , ` __dirname ` , ` __filename ` ).
155
+
118
156
### Modules loaders
119
157
120
158
Node.js has two systems for resolving a specifier and loading modules.
@@ -1369,6 +1407,7 @@ This field defines [subpath imports][] for the current package.
1369
1407
[ ES modules ] : esm.md
1370
1408
[ Node.js documentation for this section ] : https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions
1371
1409
[ Runtime Keys ] : https://runtime-keys.proposal.wintercg.org/
1410
+ [ Syntax detection ] : #syntax-detection
1372
1411
[ WinterCG ] : https://wintercg.org/
1373
1412
[ `"exports"` ] : #exports
1374
1413
[ `"imports"` ] : #imports
@@ -1378,7 +1417,6 @@ This field defines [subpath imports][] for the current package.
1378
1417
[ `"type"` ] : #type
1379
1418
[ `--conditions` / `-C` flag ] : #resolving-user-conditions
1380
1419
[ `--experimental-default-type` ] : cli.md#--experimental-default-typetype
1381
- [ `--experimental-detect-module` ] : cli.md#--experimental-detect-module
1382
1420
[ `--no-addons` flag ] : cli.md#--no-addons
1383
1421
[ `ERR_PACKAGE_PATH_NOT_EXPORTED` ] : errors.md#err_package_path_not_exported
1384
1422
[ `esm` ] : https://github.com/standard-things/esm#readme
0 commit comments