@@ -51,12 +51,13 @@ along with a reference for the [`package.json`][] fields defined by Node.js.
5151## Determining module system
5252
5353Node.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 with an ` .mjs ` extension .
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 with a ` .js ` extension 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,13 @@ field, or string input without the flag `--input-type`. This behavior is to
6768preserve backward compatibility. However, now that Node.js supports both
6869CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
6970will 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, ` import() ` expressions, or
72+ ` require() ` expressions:
7173
72- * Files ending in ` .cjs ` .
74+ * Files with a ` .cjs ` extension .
7375
74- * Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
75- top-level field [ ` "type" ` ] [ ] with a value of ` "commonjs" ` .
76+ * Files with a ` .js ` extension when the nearest parent ` package.json ` file
77+ contains a top-level field [ ` "type" ` ] [ ] with a value of ` "commonjs" ` .
7678
7779* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to ` node `
7880 via ` STDIN ` , with the flag ` --input-type=commonjs ` .
@@ -83,6 +85,48 @@ future-proof the package in case the default type of Node.js ever changes, and
8385it will also make things easier for build tools and loaders to determine how the
8486files in the package should be interpreted.
8587
88+ ### Modules loaders
89+
90+ Node.js has two systems for resolving a specifier and loading modules.
91+
92+ There is the CommonJS module loader:
93+
94+ * It is fully synchronous.
95+ * It is responsible for handling ` require() ` calls.
96+ * It is monkey patchable.
97+ * It supports [ folders as modules] [ ] .
98+ * When resolving a specifier, if no exact match is found, it will try to add
99+ extensions (` .js ` , ` .json ` , and finally ` .node ` ) and then attempt to resolve
100+ [ folders as modules] [ ] .
101+ * It treats ` .json ` as JSON text files.
102+ * ` .node ` files are interpreted as compiled addon modules loaded with
103+ ` process.dlopen() ` .
104+ * It treats all files that lack ` .json ` or ` .node ` extensions as JavaScript
105+ text files.
106+ * It cannot be used to load ECMAScript modules (although it is possible to
107+ [ load ECMASCript modules from CommonJS modules] [ ] ). When used to load a
108+ JavaScript text file that is not an ECMAScript module, it loads it as a
109+ CommonJS module.
110+
111+ There is the ECMAScript module loader:
112+
113+ * It is asynchronous.
114+ * It is responsible for handling ` import ` statements and ` import() ` expressions.
115+ * It is not monkey patchable, can be customized using [ loader hooks] [ ] .
116+ * It does not support folders as modules, directory indexes (e.g.
117+ ` './startup/index.js' ` ) must be fully specified.
118+ * It does no extension searching. A file extension must be provided
119+ when the specifier is a relative or absolute file URL.
120+ * It can load JSON modules, but an import assertion is required (behind
121+ ` --experimental-json-modules ` flag).
122+ * It accepts only ` .js ` , ` .mjs ` , and ` .cjs ` extensions for JavaScript text
123+ files.
124+ * It can be used to load JavaScript CommonJS modules. Such modules
125+ are passed through the ` es-module-lexer ` to try to identify named exports,
126+ which are available if they can be determined through static analysis.
127+ Imported CommonJS modules have their URLs converted to absolute
128+ paths and are then loaded via the CommonJS module loader.
129+
86130### ` package.json ` and file extensions
87131
88132Within a package, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
@@ -1236,6 +1280,9 @@ This field defines [subpath imports][] for the current package.
12361280[ `esm` ] : https://github.com/standard-things/esm#readme
12371281[ `package.json` ] : #nodejs-packagejson-field-definitions
12381282[ entry points ] : #package-entry-points
1283+ [ folders as modules ] : modules.md#folders-as-modules
1284+ [ load ECMASCript modules from CommonJS modules ] : modules.md#the-mjs-extension
1285+ [ loader hooks ] : esm.md#loaders
12391286[ self-reference ] : #self-referencing-a-package-using-its-name
12401287[ subpath exports ] : #subpath-exports
12411288[ subpath imports ] : #subpath-imports
0 commit comments