@@ -36,9 +36,8 @@ ES module code:
36
36
37
37
* Files ending in ` .mjs ` .
38
38
39
- * Files ending in ` .js ` , or extensionless files, when the nearest parent
40
- ` package.json ` file contains a top-level field ` "type" ` with a value of
41
- ` "module" ` .
39
+ * Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
40
+ top-level field ` "type" ` with a value of ` "module" ` .
42
41
43
42
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to
44
43
` node ` via ` STDIN ` , with the flag ` --input-type=module ` .
@@ -53,18 +52,17 @@ or when referenced by `import` statements within ES module code:
53
52
54
53
* Files ending in ` .cjs ` .
55
54
56
- * Files ending in ` .js ` , or extensionless files, when the nearest parent
57
- ` package.json ` file contains a top-level field ` "type" ` with a value of
58
- ` "commonjs" ` .
55
+ * Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
56
+ top-level field ` "type" ` with a value of ` "commonjs" ` .
59
57
60
58
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to
61
59
` node ` via ` STDIN ` , with the flag ` --input-type=commonjs ` .
62
60
63
61
### ` package.json ` ` "type" ` field
64
62
65
- Files ending with ` .js ` or lacking any extension will be loaded as ES modules
66
- when the nearest parent ` package.json ` file contains a top-level field ` "type" `
67
- with a value of ` "module" ` .
63
+ Files ending with ` .js ` will be loaded as ES modules when the nearest parent
64
+ ` package.json ` file contains a top-level field ` "type" ` with a value of
65
+ ` "module" ` .
68
66
69
67
The nearest parent ` package.json ` is defined as the first ` package.json ` found
70
68
when searching in the current folder, that folder’s parent, and so on up
@@ -84,14 +82,12 @@ node --experimental-modules my-app.js # Runs as ES module
84
82
```
85
83
86
84
If the nearest parent ` package.json ` lacks a ` "type" ` field, or contains
87
- ` "type": "commonjs" ` , extensionless and ` .js ` files are treated as CommonJS.
88
- If the volume root is reached and no ` package.json ` is found,
89
- Node.js defers to the default, a ` package.json ` with no ` "type" `
90
- field. "Extensionless" refers to file paths which do not contain
91
- an extension as opposed to optionally dropping a file extension in a specifier.
85
+ ` "type": "commonjs" ` , ` .js ` files are treated as CommonJS. If the volume root is
86
+ reached and no ` package.json ` is found, Node.js defers to the default, a
87
+ ` package.json ` with no ` "type" ` field.
92
88
93
- ` import ` statements of ` .js ` and extensionless files are treated as ES modules
94
- if the nearest parent ` package.json ` contains ` "type": "module" ` .
89
+ ` import ` statements of ` .js ` files are treated as ES modules if the nearest
90
+ parent ` package.json ` contains ` "type": "module" ` .
95
91
96
92
``` js
97
93
// my-app.js, part of the same example as above
@@ -109,14 +105,13 @@ as ES modules and `.cjs` files are always treated as CommonJS.
109
105
110
106
### Package Scope and File Extensions
111
107
112
- A folder containing a ` package.json ` file, and all subfolders below that
113
- folder down until the next folder containing another ` package.json ` , is
114
- considered a _ package scope_ . The ` "type" ` field defines how ` .js ` and
115
- extensionless files should be treated within a particular ` package.json ` file’s
116
- package scope. Every package in a project’s ` node_modules ` folder contains its
117
- own ` package.json ` file, so each project’s dependencies have their own package
118
- scopes. A ` package.json ` lacking a ` "type" ` field is treated as if it contained
119
- ` "type": "commonjs" ` .
108
+ A folder containing a ` package.json ` file, and all subfolders below that folder
109
+ down until the next folder containing another ` package.json ` , is considered a
110
+ _ package scope_ . The ` "type" ` field defines how ` .js ` files should be treated
111
+ within a particular ` package.json ` file’s package scope. Every package in a
112
+ project’s ` node_modules ` folder contains its own ` package.json ` file, so each
113
+ project’s dependencies have their own package scopes. A ` package.json ` lacking a
114
+ ` "type" ` field is treated as if it contained ` "type": "commonjs" ` .
120
115
121
116
The package scope applies not only to initial entry points (`node
122
117
--experimental-modules my-app.js` ) but also to files referenced by ` import`
@@ -1610,12 +1605,10 @@ _defaultEnv_ is the conditional environment name priority array,
1610
1605
> 1. If _url_ ends in _".cjs"_, then
1611
1606
> 1. Return _"commonjs"_.
1612
1607
> 1. If _pjson?.type_ exists and is _"module"_, then
1613
- > 1. If _url_ ends in _".js"_ or lacks a file extension , then
1608
+ > 1. If _url_ ends in _".js"_, then
1614
1609
> 1. Return _"module"_.
1615
1610
> 1. Throw an _Unsupported File Extension_ error.
1616
1611
> 1. Otherwise,
1617
- > 1. If _url_ lacks a file extension, then
1618
- > 1. Return _"commonjs"_.
1619
1612
> 1. Throw an _Unsupported File Extension_ error.
1620
1613
1621
1614
**READ_PACKAGE_SCOPE**(_url_)
0 commit comments