@@ -51,12 +51,13 @@ along with a reference for the [`package.json`][] fields defined by Node.js.
51
51
## Determining module system
52
52
53
53
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:
55
56
56
- * Files ending in ` .mjs ` .
57
+ * Files with an ` .mjs ` extension .
57
58
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" ` .
60
61
61
62
* Strings passed in as an argument to ` --eval ` , or piped to ` node ` via ` STDIN ` ,
62
63
with the flag ` --input-type=module ` .
@@ -67,12 +68,13 @@ field, or string input without the flag `--input-type`. This behavior is to
67
68
preserve backward compatibility. However, now that Node.js supports both
68
69
CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
69
70
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, ` import() ` expressions, or
72
+ ` require() ` expressions:
71
73
72
- * Files ending in ` .cjs ` .
74
+ * Files with a ` .cjs ` extension .
73
75
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" ` .
76
78
77
79
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to ` node `
78
80
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
83
85
it will also make things easier for build tools and loaders to determine how the
84
86
files in the package should be interpreted.
85
87
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
+
86
130
### ` package.json ` and file extensions
87
131
88
132
Within a package, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
@@ -1236,6 +1280,9 @@ This field defines [subpath imports][] for the current package.
1236
1280
[ `esm` ] : https://github.com/standard-things/esm#readme
1237
1281
[ `package.json` ] : #nodejs-packagejson-field-definitions
1238
1282
[ 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
1239
1286
[ self-reference ] : #self-referencing-a-package-using-its-name
1240
1287
[ subpath exports ] : #subpath-exports
1241
1288
[ subpath imports ] : #subpath-imports
0 commit comments