@@ -69,14 +69,14 @@ expressions:
6969* Strings passed in as an argument to ` --eval ` , or piped to ` node ` via ` STDIN ` ,
7070 with the flag ` --input-type=module ` .
7171
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 ] [ ] .
8080
8181Node.js will treat the following as [ CommonJS] [ ] when passed to ` node ` as the
8282initial 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
115115things easier for build tools and loaders to determine how the files in the
116116package should be interpreted.
117117
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+
118156### Modules loaders
119157
120158Node.js has two systems for resolving a specifier and loading modules.
@@ -1369,6 +1407,7 @@ This field defines [subpath imports][] for the current package.
13691407[ ES modules ] : esm.md
13701408[ Node.js documentation for this section ] : https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions
13711409[ Runtime Keys ] : https://runtime-keys.proposal.wintercg.org/
1410+ [ Syntax detection ] : #syntax-detection
13721411[ WinterCG ] : https://wintercg.org/
13731412[ `"exports"` ] : #exports
13741413[ `"imports"` ] : #imports
@@ -1378,7 +1417,6 @@ This field defines [subpath imports][] for the current package.
13781417[ `"type"` ] : #type
13791418[ `--conditions` / `-C` flag ] : #resolving-user-conditions
13801419[ `--experimental-default-type` ] : cli.md#--experimental-default-typetype
1381- [ `--experimental-detect-module` ] : cli.md#--experimental-detect-module
13821420[ `--no-addons` flag ] : cli.md#--no-addons
13831421[ `ERR_PACKAGE_PATH_NOT_EXPORTED` ] : errors.md#err_package_path_not_exported
13841422[ `esm` ] : https://github.com/standard-things/esm#readme
0 commit comments