@@ -109,11 +109,21 @@ provides interoperability between them and its original module format,
109
109
110
110
Node.js has two module systems: [ CommonJS] [ ] modules and ECMAScript modules.
111
111
112
- Authors can tell Node.js to use the ECMAScript modules loader via the ` .mjs `
113
- file extension, the ` package.json ` [ ` "type" ` ] [ ] field, the [ ` --input-type ` ] [ ]
114
- flag, or the [ ` --experimental-default-type ` ] [ ] flag. Outside of those cases,
115
- Node.js will use the CommonJS module loader. See [ Determining module system] [ ]
116
- for more details.
112
+ Authors can tell Node.js to interpret JavaScript as an ES module via the ` .mjs `
113
+ file extension, the ` package.json ` [ ` "type" ` ] [ ] field with a value ` "module" ` ,
114
+ the [ ` --input-type ` ] [ ] flag with a value of ` "module" ` , or the
115
+ [ ` --experimental-default-type ` ] [ ] flag with a value of ` "module" ` . These are
116
+ explicit markers of code being intended to run as an ES module.
117
+
118
+ Inversely, authors can tell Node.js to interpret JavaScript as CommonJS via the
119
+ ` .cjs ` file extension, the ` package.json ` [ ` "type" ` ] [ ] field with a value
120
+ ` "commonjs" ` , the [ ` --input-type ` ] [ ] flag with a value of ` "commonjs" ` , or the
121
+ [ ` --experimental-default-type ` ] [ ] flag with a value of ` "commonjs" ` .
122
+
123
+ When code lacks explicit markers for either module system, Node.js will inspect
124
+ the source code of a module to look for ES module syntax. If such syntax is
125
+ found, Node.js will run the code as an ES module; otherwise it will run the
126
+ module as CommonJS. See [ Determining module system] [ ] for more details.
117
127
118
128
<!-- Anchors to make sure old links find a target -->
119
129
@@ -1019,18 +1029,33 @@ _isImports_, _conditions_)
1019
1029
> 1. Return _"commonjs"_.
1020
1030
> 4. If _url_ ends in _".json"_, then
1021
1031
> 1. Return _"json"_.
1022
- > 5. Let _packageURL_ be the result of **LOOKUP\_ PACKAGE\_ SCOPE**(_url_).
1023
- > 6. Let _pjson_ be the result of **READ\_ PACKAGE\_ JSON**(_packageURL_).
1024
- > 7. If _pjson?.type_ exists and is _"module"_, then
1025
- > 1. If _url_ ends in _".js"_ or has no file extension, then
1026
- > 1. If ` -- experimental- wasm- modules` is enabled and the file at _url_
1027
- > contains the header for a WebAssembly module, then
1028
- > 1. Return _"wasm"_.
1029
- > 2. Otherwise,
1030
- > 1. Return _"module"_.
1031
- > 2. Return **undefined**.
1032
- > 8. Otherwise,
1033
- > 1. Return **undefined**.
1032
+ > 5. If ` -- experimental- wasm- modules` is enabled and _url_ ends in
1033
+ > _".wasm"_, then
1034
+ > 1. Return _"wasm"_.
1035
+ > 6. Let _packageURL_ be the result of **LOOKUP\_ PACKAGE\_ SCOPE**(_url_).
1036
+ > 7. Let _pjson_ be the result of **READ\_ PACKAGE\_ JSON**(_packageURL_).
1037
+ > 8. Let _packageType_ be **null**.
1038
+ > 9. If _pjson?.type_ is _"module"_ or _"commonjs"_, then
1039
+ > 1. Set _packageType_ to _pjson.type_.
1040
+ > 10. If _url_ ends in _".js"_, then
1041
+ > 1. If _packageType_ is not **null**, then
1042
+ > 1. Return _packageType_.
1043
+ > 2. If ` -- experimental- detect- module ` is enabled and the source of
1044
+ > module contains static import or export syntax, then
1045
+ > 1. Return _"module"_.
1046
+ > 3. Return _"commonjs"_.
1047
+ > 11. If _url_ does not have any extension, then
1048
+ > 1. If _packageType_ is _"module"_ and ` -- experimental- wasm- modules` is
1049
+ > enabled and the file at _url_ contains the header for a WebAssembly
1050
+ > module, then
1051
+ > 1. Return _"wasm"_.
1052
+ > 2. If _packageType_ is not **null**, then
1053
+ > 1. Return _packageType_.
1054
+ > 3. If ` -- experimental- detect- module ` is enabled and the source of
1055
+ > module contains static import or export syntax, then
1056
+ > 1. Return _"module"_.
1057
+ > 4. Return _"commonjs"_.
1058
+ > 12. Return **undefined** (will throw during load phase).
1034
1059
1035
1060
**LOOKUP\_ PACKAGE\_ SCOPE**(_url_)
1036
1061
0 commit comments