@@ -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 whose name ends in ` .mjs ` .
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 whose name ends in ` .js ` 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,12 @@ 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 or ` import() ` expressions :
71
72
72
- * Files ending in ` .cjs ` .
73
+ * Files whose name ends in ` .cjs ` .
73
74
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" ` .
76
77
77
78
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to ` node `
78
79
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
83
84
it will also make things easier for build tools and loaders to determine how the
84
85
files in the package should be interpreted.
85
86
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
+
86
104
### ` package.json ` and file extensions
87
105
88
106
Within a package, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
0 commit comments