Skip to content

Commit 1b1159a

Browse files
committed
doc: clarify module system selection
Refs: nodejs#41345 (comment)
1 parent f1658bd commit 1b1159a

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

doc/api/esm.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ provides interoperability between them and its original module format,
9494

9595
<!-- type=misc -->
9696

97-
Node.js treats JavaScript code as CommonJS modules by default.
98-
Authors can tell Node.js to treat JavaScript code as ECMAScript modules
97+
Node.js has two module systems: [CommonJS][] modules and ECMAScript modules.
98+
99+
Authors can tell Node.js to use the ECMAScript modules loader
99100
via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the
100-
`--input-type` flag. See
101-
[Modules: Packages](packages.md#determining-module-system) for more
102-
details.
101+
[`--input-type`][] flag. Outside of those cases, Node.js will use the CommonJS
102+
module loader. See [Determining module system][] for more details.
103103

104104
<!-- Anchors to make sure old links find a target -->
105105

@@ -1425,6 +1425,7 @@ success!
14251425
[CommonJS]: modules.md
14261426
[Conditional exports]: packages.md#conditional-exports
14271427
[Core modules]: modules.md#core-modules
1428+
[Determining module system]: packages.md#determining-module-system
14281429
[Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
14291430
[ECMAScript Top-Level `await` proposal]: https://github.com/tc39/proposal-top-level-await/
14301431
[ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration
@@ -1437,6 +1438,7 @@ success!
14371438
[WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script
14381439
[`"exports"`]: packages.md#exports
14391440
[`"type"`]: packages.md#type
1441+
[`--input-type`]: cli.md#--input-typetype
14401442
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
14411443
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
14421444
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

doc/api/modules.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,18 @@ module.exports = class Square {
6161
};
6262
```
6363

64-
The module system is implemented in the `require('module')` module.
64+
The CommonJS module system is implemented in the [`module` core module][].
65+
66+
## Enabling
67+
68+
<!-- type=misc -->
69+
70+
Node.js has two module systems: CommonJS modules and [ECMAScript modules][].
71+
72+
Authors can tell Node.js to use the ECMAScript modules loader
73+
via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the
74+
[`--input-type`][] flag. Outside of those cases, Node.js will use the CommonJS
75+
module loader. See [Determining module system][] for more details.
6576

6677
## Accessing the main module
6778

@@ -1047,13 +1058,16 @@ This section was moved to
10471058
[ECMAScript Modules]: esm.md
10481059
[GLOBAL_FOLDERS]: #loading-from-the-global-folders
10491060
[`"main"`]: packages.md#main
1061+
[`"type"`]: packages.md#type
1062+
[`--input-type`]: cli.md#--input-typetype
10501063
[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
10511064
[`Error`]: errors.md#class-error
10521065
[`__dirname`]: #__dirname
10531066
[`__filename`]: #__filename
10541067
[`import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
10551068
[`module.children`]: #modulechildren
10561069
[`module.id`]: #moduleid
1070+
[`module` core module]: module.md
10571071
[`module` object]: #the-module-object
10581072
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
10591073
[`path.dirname()`]: path.md#pathdirnamepath

doc/api/packages.md

+26-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ along with a reference for the [`package.json`][] fields defined by Node.js.
5151
## Determining module system
5252

5353
Node.js will treat the following as [ES modules][] when passed to `node` as the
54-
initial input, or when referenced by `import` statements within ES module code:
54+
initial input, or when referenced by `import` statements or `import()`
55+
expressions:
5556

56-
* Files ending in `.mjs`.
57+
* Files whose name ends in `.mjs`.
5758

58-
* Files ending in `.js` when the nearest parent `package.json` file contains a
59-
top-level [`"type"`][] field with a value of `"module"`.
59+
* Files whose name ends in `.js` when the nearest parent `package.json` file
60+
contains a top-level [`"type"`][] field with a value of `"module"`.
6061

6162
* Strings passed in as an argument to `--eval`, or piped to `node` via `STDIN`,
6263
with the flag `--input-type=module`.
@@ -67,12 +68,12 @@ field, or string input without the flag `--input-type`. This behavior is to
6768
preserve backward compatibility. However, now that Node.js supports both
6869
CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
6970
will treat the following as CommonJS when passed to `node` as the initial input,
70-
or when referenced by `import` statements within ES module code:
71+
or when referenced by `import` statements or `import()` expressions:
7172

72-
* Files ending in `.cjs`.
73+
* Files whose name ends in `.cjs`.
7374

74-
* Files ending in `.js` when the nearest parent `package.json` file contains a
75-
top-level field [`"type"`][] with a value of `"commonjs"`.
75+
* Files whose name ends in `.js` when the nearest parent `package.json` file
76+
contains a top-level field [`"type"`][] with a value of `"commonjs"`.
7677

7778
* Strings passed in as an argument to `--eval` or `--print`, or piped to `node`
7879
via `STDIN`, with the flag `--input-type=commonjs`.
@@ -83,6 +84,23 @@ future-proof the package in case the default type of Node.js ever changes, and
8384
it will also make things easier for build tools and loaders to determine how the
8485
files in the package should be interpreted.
8586

87+
Node.js will refuse to load the following when passed to `node` as the
88+
initial input and the nearest parent `package.json` file contains a top-level
89+
[`"type"`][] field with a value of `"module"`:
90+
91+
* Files whose name doesn't end in `.js`, `.mjs`, `.cjs`, or `.wasm`.
92+
93+
Passing to `node` as the initial input when the nearest parent `package.json`
94+
file contains a top-level [`"type"`][] field with a value of `"commonjs"`, or
95+
when referenced by `require()` calls:
96+
97+
* Files whose name ends in `.node` are interpreted as
98+
compiled addon modules loaded with `process.dlopen()`.
99+
100+
* Files whose name ends in `.json` are parsed as JSON text files.
101+
102+
* Any other files will be treated as a [CommonJS][] module.
103+
86104
### `package.json` and file extensions
87105

88106
Within a package, the [`package.json`][] [`"type"`][] field defines how

0 commit comments

Comments
 (0)