@@ -33,8 +33,7 @@ initial input, or when referenced by `import` statements within ES module code:
33
33
34
34
* Files ending in ` .mjs ` .
35
35
36
- * Files ending in ` .js ` , or extensionless files when run as main entry points on
37
- the command line, when the nearest parent ` package.json ` file contains a
36
+ * Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
38
37
top-level field ` "type" ` with a value of ` "module" ` .
39
38
40
39
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to
@@ -50,18 +49,17 @@ or when referenced by `import` statements within ES module code:
50
49
51
50
* Files ending in ` .cjs ` .
52
51
53
- * Files ending in ` .js ` , or extensionless files when run as main entry points on
54
- the command line, when the nearest parent ` package.json ` file contains a
52
+ * Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
55
53
top-level field ` "type" ` with a value of ` "commonjs" ` .
56
54
57
55
* Strings passed in as an argument to ` --eval ` or ` --print ` , or piped to
58
56
` node ` via ` STDIN ` , with the flag ` --input-type=commonjs ` .
59
57
60
58
### ` package.json ` ` "type" ` field
61
59
62
- Files ending with ` .js ` or lacking any extension will be loaded as ES modules
63
- when the nearest parent ` package.json ` file contains a top-level field ` "type" `
64
- with a value of ` "module" ` .
60
+ Files ending with ` .js ` will be loaded as ES modules when the nearest parent
61
+ ` package.json ` file contains a top-level field ` "type" ` with a value of
62
+ ` "module" ` .
65
63
66
64
The nearest parent ` package.json ` is defined as the first ` package.json ` found
67
65
when searching in the current folder, that folderβs parent, and so on up
@@ -81,14 +79,12 @@ node my-app.js # Runs as ES module
81
79
```
82
80
83
81
If the nearest parent ` package.json ` lacks a ` "type" ` field, or contains
84
- ` "type": "commonjs" ` , extensionless and ` .js ` files are treated as CommonJS.
85
- If the volume root is reached and no ` package.json ` is found,
86
- Node.js defers to the default, a ` package.json ` with no ` "type" `
87
- field. "Extensionless" refers to file paths which do not contain
88
- an extension as opposed to optionally dropping a file extension in a specifier.
82
+ ` "type": "commonjs" ` , ` .js ` files are treated as CommonJS. If the volume root is
83
+ reached and no ` package.json ` is found, Node.js defers to the default, a
84
+ ` package.json ` with no ` "type" ` field.
89
85
90
- ` import ` statements of ` .js ` and extensionless files are treated as ES modules
91
- if the nearest parent ` package.json ` contains ` "type": "module" ` .
86
+ ` import ` statements of ` .js ` files are treated as ES modules if the nearest
87
+ parent ` package.json ` contains ` "type": "module" ` .
92
88
93
89
``` js
94
90
// my-app.js, part of the same example as above
@@ -106,14 +102,13 @@ as ES modules and `.cjs` files are always treated as CommonJS.
106
102
107
103
### Package Scope and File Extensions
108
104
109
- A folder containing a ` package.json ` file, and all subfolders below that
110
- folder down until the next folder containing another ` package.json ` , is
111
- considered a _ package scope_ . The ` "type" ` field defines how ` .js ` and
112
- extensionless files should be treated within a particular ` package.json ` fileβs
113
- package scope. Every package in a projectβs ` node_modules ` folder contains its
114
- own ` package.json ` file, so each projectβs dependencies have their own package
115
- scopes. A ` package.json ` lacking a ` "type" ` field is treated as if it contained
116
- ` "type": "commonjs" ` .
105
+ A folder containing a ` package.json ` file, and all subfolders below that folder
106
+ down until the next folder containing another ` package.json ` , is considered a
107
+ _ package scope_ . The ` "type" ` field defines how ` .js ` files should be treated
108
+ within a particular ` package.json ` fileβs package scope. Every package in a
109
+ projectβs ` node_modules ` folder contains its own ` package.json ` file, so each
110
+ projectβs dependencies have their own package scopes. A ` package.json ` lacking a
111
+ ` "type" ` field is treated as if it contained ` "type": "commonjs" ` .
117
112
118
113
The package scope applies not only to initial entry points (` node my-app.js ` )
119
114
but also to files referenced by ` import ` statements and ` import() ` expressions.
@@ -1042,8 +1037,7 @@ a URL should be interpreted. This can be one of the following:
1042
1037
` ` ` js
1043
1038
/**
1044
1039
* @param {string} url
1045
- * @param {object} context
1046
- * @param {string} context.parentURL
1040
+ * @param {object} context (currently empty)
1047
1041
* @param {function} defaultGetFormat
1048
1042
* @returns {object} response
1049
1043
* @returns {string} response.format
@@ -1367,15 +1361,13 @@ updates.
1367
1361
In the following algorithms, all subroutine errors are propagated as errors
1368
1362
of these top-level routines unless stated otherwise.
1369
1363
1370
- _isMain_ is **true** when resolving the Node.js application entry point.
1371
-
1372
1364
_defaultEnv_ is the conditional environment name priority array,
1373
1365
` [" node" , " import" ]` .
1374
1366
1375
1367
<details>
1376
1368
<summary>Resolver algorithm specification</summary>
1377
1369
1378
- **ESM_RESOLVE**(_specifier_, _parentURL_, _isMain_ )
1370
+ **ESM_RESOLVE**(_specifier_, _parentURL_)
1379
1371
1380
1372
> 1. Let _resolvedURL_ be **undefined**.
1381
1373
> 1. If _specifier_ is a valid URL, then
@@ -1396,7 +1388,7 @@ _defaultEnv_ is the conditional environment name priority array,
1396
1388
> 1. If the file at _resolvedURL_ does not exist, then
1397
1389
> 1. Throw a _Module Not Found_ error.
1398
1390
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
1399
- > 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_, _isMain_ ).
1391
+ > 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
1400
1392
> 1. Load _resolvedURL_ as module format, _format_.
1401
1393
1402
1394
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
@@ -1549,7 +1541,7 @@ _defaultEnv_ is the conditional environment name priority array,
1549
1541
> 1. Return _resolved_.
1550
1542
> 1. Throw a _Module Not Found_ error.
1551
1543
1552
- **ESM_FORMAT**(_url_, _isMain_ )
1544
+ **ESM_FORMAT**(_url_)
1553
1545
1554
1546
> 1. Assert: _url_ corresponds to an existing file.
1555
1547
> 1. Let _pjson_ be the result of **READ_PACKAGE_SCOPE**(_url_).
@@ -1558,12 +1550,10 @@ _defaultEnv_ is the conditional environment name priority array,
1558
1550
> 1. If _url_ ends in _".cjs"_, then
1559
1551
> 1. Return _"commonjs"_.
1560
1552
> 1. If _pjson?.type_ exists and is _"module"_, then
1561
- > 1. If _isMain_ is **true** or _url_ ends in _".js"_, then
1553
+ > 1. If _url_ ends in _".js"_, then
1562
1554
> 1. Return _"module"_.
1563
1555
> 1. Throw an _Unsupported File Extension_ error.
1564
1556
> 1. Otherwise,
1565
- > 1. If _isMain_ is **true**, then
1566
- > 1. Return _"commonjs"_.
1567
1557
> 1. Throw an _Unsupported File Extension_ error.
1568
1558
1569
1559
**READ_PACKAGE_SCOPE**(_url_)
0 commit comments