4
4
5
5
## Introduction
6
6
7
- A package is a folder described by a ` package.json ` file.
7
+ A package is a folder tree described by a ` package.json ` file. The package
8
+ consists of the folder containing the ` package.json ` file and all subfolders
9
+ until the next folder containing another ` package.json ` file, or a folder
10
+ named ` node_modules ` .
8
11
9
- A folder containing a ` package.json ` file, and all subfolders below that folder
10
- until the next folder containing another ` package.json ` file, are a _ package
11
- scope_ .
12
+ This page provides guidance for package authors writing ` package.json ` files
13
+ along with a reference for the [ ` package.json ` ] [ ] fields defined by Node.js.
12
14
13
15
## Determining module system
14
16
@@ -18,7 +20,7 @@ initial input, or when referenced by `import` statements within ES module code:
18
20
* Files ending in ` .mjs ` .
19
21
20
22
* Files ending in ` .js ` when the nearest parent ` package.json ` file contains a
21
- top-level field [ ` "type" ` ] [ ] with a value of ` "module" ` .
23
+ top-level [ ` "type" ` ] [ ] field with a value of ` "module" ` .
22
24
23
25
* Strings passed in as an argument to ` --eval ` , or piped to ` node ` via ` STDIN ` ,
24
26
with the flag ` --input-type=module ` .
@@ -45,29 +47,25 @@ future-proof the package in case the default type of Node.js ever changes, and
45
47
it will also make things easier for build tools and loaders to determine how the
46
48
files in the package should be interpreted.
47
49
48
- ### Package scope and file extensions
50
+ ### ` package.json ` and file extensions
49
51
50
- A folder containing a ` package.json ` file, and all subfolders below that folder
51
- until the next folder containing another [ ` package.json ` ] [ ] , are a
52
- _ package scope_ . Package scopes do not carry through ` node_modules ` folders.
53
-
54
- Within a package scope, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
52
+ Within a package, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
55
53
Node.js should interpret ` .js ` files. If a ` package.json ` file does not have a
56
54
` "type" ` field, ` .js ` files are treated as [ CommonJS] [ ] .
57
55
58
56
A ` package.json ` ` "type" ` value of ` "module" ` tells Node.js to interpret ` .js `
59
- files within that package scope as using [ ES module] [ ] syntax.
57
+ files within that package as using [ ES module] [ ] syntax.
60
58
61
- The package scope applies not only to initial entry points (` node my-app.js ` )
59
+ The ` "type" ` field applies not only to initial entry points (` node my-app.js ` )
62
60
but also to files referenced by ` import ` statements and ` import() ` expressions.
63
61
64
62
``` js
65
- // my-app.js, in an ES module package scope because there is a package.json
63
+ // my-app.js, treated as an ES module because there is a package.json
66
64
// file in the same folder with "type": "module".
67
65
68
66
import ' ./startup/init.js' ;
69
67
// Loaded as ES module since ./startup contains no package.json file,
70
- // and therefore inherits the ES module package scope from one level up.
68
+ // and therefore inherits the "type" value from one level up.
71
69
72
70
import ' commonjs-package' ;
73
71
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
@@ -79,10 +77,10 @@ import './node_modules/commonjs-package/index.js';
79
77
```
80
78
81
79
Files ending with ` .mjs ` are always loaded as [ ES modules] [ ] regardless of
82
- package scope .
80
+ the nearest parent ` package.json ` .
83
81
84
- Files ending with ` .cjs ` are always loaded as [ CommonJS] [ ] regardless of package
85
- scope .
82
+ Files ending with ` .cjs ` are always loaded as [ CommonJS] [ ] regardless of the
83
+ nearest parent ` package.json ` .
86
84
87
85
``` js
88
86
import ' ./legacy-file.cjs' ;
@@ -93,17 +91,17 @@ import 'commonjs-package/src/index.mjs';
93
91
```
94
92
95
93
The ` .mjs ` and ` .cjs ` extensions may be used to mix types within the same
96
- package scope :
94
+ package:
97
95
98
- * Within a ` "type": "module" ` package scope , Node.js can be instructed to
96
+ * Within a ` "type": "module" ` package, Node.js can be instructed to
99
97
interpret a particular file as [ CommonJS] [ ] by naming it with a ` .cjs `
100
98
extension (since both ` .js ` and ` .mjs ` files are treated as ES modules within
101
- a ` "module" ` package scope ).
99
+ a ` "module" ` package).
102
100
103
- * Within a ` "type": "commonjs" ` package scope , Node.js can be instructed to
101
+ * Within a ` "type": "commonjs" ` package, Node.js can be instructed to
104
102
interpret a particular file as an [ ES module] [ ] by naming it with an ` .mjs `
105
103
extension (since both ` .js ` and ` .cjs ` files are treated as CommonJS within a
106
- ` "commonjs" ` package scope ).
104
+ ` "commonjs" ` package).
107
105
108
106
### ` --input-type ` flag
109
107
@@ -211,10 +209,10 @@ To set the main entry point for a package, it is advisable to define both
211
209
}
212
210
```
213
211
214
- The benefit of doing this is that when using the [ ` "exports" ` ] [ ] field all
215
- subpaths of the package will no longer be available to importers under
216
- ` require('pkg/subpath.js') ` , and instead they will get a new error,
217
- ` ERR_PACKAGE_PATH_NOT_EXPORTED ` .
212
+ When defining the [ ` "exports" ` ] [ ] field, all subpaths of the package will be
213
+ encapsulated and no longer available to importers. For example,
214
+ ` require('pkg/subpath.js') ` would throw an [ ` ERR_PACKAGE_PATH_NOT_EXPORTED ` ] [ ]
215
+ error .
218
216
219
217
This encapsulation of exports provides more reliable guarantees
220
218
about package interfaces for tools and when handling semver upgrades for a
@@ -295,24 +293,6 @@ treating the right hand side target pattern as a `**` glob against the list of
295
293
files within the package. Because ` node_modules ` paths are forbidden in exports
296
294
targets, this expansion is dependent on only the files of the package itself.
297
295
298
- ### Package exports fallbacks
299
-
300
- > Stability: 1 - Experimental
301
-
302
- For possible new specifier support in future, array fallbacks are
303
- supported for all invalid specifiers:
304
-
305
- ``` json
306
- {
307
- "exports" : {
308
- "./submodule" : [" not:valid" , " ./submodule.js" ]
309
- }
310
- }
311
- ```
312
-
313
- Since ` "not:valid" ` is not a valid specifier, ` "./submodule.js" ` is used
314
- instead as the fallback, as if it were the only target.
315
-
316
296
### Exports sugar
317
297
318
298
> Stability: 1 - Experimental
@@ -560,9 +540,10 @@ could be intended only for other environments such as browsers. Such a package
560
540
would be usable by any version of Node.js, since ` import ` can refer to CommonJS
561
541
files; but it would not provide any of the advantages of using ES module syntax.
562
542
563
- A package could also switch from CommonJS to ES module syntax in a breaking
564
- change version bump. This has the disadvantage that the newest version
565
- of the package would only be usable in ES module-supporting versions of Node.js.
543
+ A package could also switch from CommonJS to ES module syntax in a [ breaking
544
+ change] ( https://semver.org/ ) version bump. This has the disadvantage that the
545
+ newest version of the package would only be usable in ES module-supporting
546
+ versions of Node.js.
566
547
567
548
Every pattern has tradeoffs, but there are two broad approaches that satisfy the
568
549
following conditions:
@@ -773,6 +754,19 @@ This section describes the fields used by the Node.js runtime. Other tools (such
773
754
as [npm](https: // docs.npmjs.com/creating-a-package-json-file)) may use
774
755
additional fields which are ignored by Node .js and not documented here.
775
756
757
+ The following fields in ` package.json` files are used in Node .js :
758
+
759
+ * [` "name"` ][] - Relevant when using named imports within a package . Also used
760
+ by package managers as the name of the package .
761
+ * [` "type"` ][] - The package type determining whether to load ` .js` files as
762
+ CommonJS or ES modules.
763
+ * [` "exports"` ][] - Package exports and conditional exports . When present,
764
+ limits which submodules may be loaded from within the package .
765
+ * [` "main"` ][] - The default module when loading the package , if exports is not
766
+ specified, and in versions of Node .js prior to the introduction of exports .
767
+ * [` "imports"` ][] - Package imports, for use by modules within the package
768
+ itself.
769
+
776
770
### ` "name"`
777
771
<!-- YAML
778
772
added:
@@ -815,7 +809,7 @@ changes:
815
809
* Type: {string}
816
810
817
811
The ` "type"` field defines the module format that Node .js will use for all
818
- ` .js` files within a particular ` package.json` file’s [ package scope][] .
812
+ ` .js` files that have that ` package.json` file as their nearest parent .
819
813
820
814
Files ending with ` .js` will be loaded as ES modules when the nearest parent
821
815
` package.json` file contains a top- level field ` "type"` with a value of
@@ -839,8 +833,8 @@ node my-app.js # Runs as ES module
839
833
840
834
If the nearest parent ` package.json` lacks a ` "type"` field, or contains
841
835
` "type": "commonjs"` , ` .js` files are treated as [CommonJS][]. If the volume
842
- root is reached and no ` package.json` is found, Node .js defers to the default
843
- treatment as [CommonJS][].
836
+ root is reached and no ` package.json` is found, ` .js` files are treated as
837
+ [CommonJS][].
844
838
845
839
` import` statements of ` .js` files are treated as ES modules if the nearest
846
840
parent ` package.json` contains ` "type": "module"` .
@@ -957,26 +951,27 @@ where `import '#dep'` would now get the resolution of the external package
957
951
` dep-node-native` (including its exports in turn), and instead get the local
958
952
file ` ./dep-polyfill.js` relative to the package in other environments.
959
953
960
- Unlike the exports field, import maps permit mapping to external packages
961
- because this provides an important use case for conditional loading and also can
962
- be done without the risk of cycles, unlike for exports .
954
+ Unlike the ` "exports"` field, import maps permit mapping to external packages,
955
+ providing an important use case for conditional loading scenarios.
963
956
964
957
Apart from the above, the resolution rules for the imports field are otherwise
965
958
analogous to the exports field.
966
959
967
960
[Babel]: https: // babeljs.io/
968
961
[Conditional exports ]: #packages_conditional_exports
969
962
[CommonJS]: modules .html
970
- [entry points]: #packages_package_entry_points
971
- [` esm` ]: https: // github.com/standard-things/esm#readme
963
+ [` ERR_PACKAGE_PATH_NOT_EXPORTED` ]: errors .html #errors_err_package_path_not_exported
972
964
[ES modules]: esm .html
973
965
[ES module ]: esm .html
966
+ [` esm` ]: https: // github.com/standard-things/esm#readme
974
967
[` "exports"` ]: #packages_exports
975
968
[` "main"` ]: #packages_main
969
+ [` "name"` ]: #packages_name
970
+ [` "imports"` ]: #packages_imports
971
+ [` "type"` ]: #packages_type
972
+ [entry points]: #packages_package_entry_points
976
973
[` package.json` ]: #packages_node_js_package_json_field_definitions
977
- [package scope]: #packages_package_scope_and_file_extensions
978
974
[self - reference]: #packages_self_referencing_a_package_using_its_name
979
975
[subpath exports ]: #packages_subpath_exports
980
976
[the full specifier path]: modules_esm .html #modules_esm_mandatory_file_extensions
981
977
[the dual CommonJS/ ES module packages section]: #packages_dual_commonjs_es_module_packages
982
- [` "type"` ]: #packages_type
0 commit comments