@@ -531,8 +531,17 @@ For example, a package that wants to provide different ES module exports for
531
531
}
532
532
```
533
533
534
- Node.js implements the following conditions:
534
+ Node.js implements the following conditions, listed in order from most
535
+ specific to least specific as conditions should be defined:
535
536
537
+ * ` "node-addons" ` - similar to ` "node" ` and matches for any Node.js environment.
538
+ This condition can be used to provide an entry point which uses native C++
539
+ addons as opposed to an entry point which is more universal and doesn't rely
540
+ on native addons. This condition can be disabled via the
541
+ [ ` --no-addons ` flag] [ ] .
542
+ * ` "node" ` - matches for any Node.js environment. Can be a CommonJS or ES
543
+ module file. _ In most cases explicitly calling out the Node.js platform is
544
+ not necessary._
536
545
* ` "import" ` - matches when the package is loaded via ` import ` or
537
546
` import() ` , or via any top-level import or resolve operation by the
538
547
ECMAScript module loader. Applies regardless of the module format of the
@@ -543,14 +552,6 @@ Node.js implements the following conditions:
543
552
formats include CommonJS, JSON, and native addons but not ES modules as
544
553
` require() ` doesn't support them. _ Always mutually exclusive with
545
554
` "import" ` ._
546
- * ` "node" ` - matches for any Node.js environment. Can be a CommonJS or ES
547
- module file. _ This condition should always come after ` "import" ` or
548
- ` "require" ` ._
549
- * ` "node-addons" ` - similar to ` "node" ` and matches for any Node.js environment.
550
- This condition can be used to provide an entry point which uses native C++
551
- addons as opposed to an entry point which is more universal and doesn't rely
552
- on native addons. This condition can be disabled via the
553
- [ ` --no-addons ` flag] [ ] .
554
555
* ` "default" ` - the generic fallback that always matches. Can be a CommonJS
555
556
or ES module file. _ This condition should always come last._
556
557
@@ -562,6 +563,12 @@ least specific in object order_.
562
563
Using the ` "import" ` and ` "require" ` conditions can lead to some hazards,
563
564
which are further explained in [ the dual CommonJS/ES module packages section] [ ] .
564
565
566
+ The ` "node-addons" ` condition can be used to provide an entry point which
567
+ uses native C++ addons. However, this condition can be disabled via the
568
+ [ ` --no-addons ` flag] [ ] . When using ` "node-addons" ` , it's recommended to treat
569
+ ` "default" ` as an enhancement that provides a more universal entry point, e.g.
570
+ using WebAssembly instead of a native addon.
571
+
565
572
Conditional exports can also be extended to exports subpaths, for example:
566
573
567
574
``` json
@@ -635,40 +642,30 @@ exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`,
635
642
636
643
Any number of custom conditions can be set with repeat flags.
637
644
638
- ### Conditions Definitions
645
+ ### Community Conditions Definitions
639
646
640
- The ` "import" ` , ` "require" ` , ` "node" ` , ` "node-addons" ` and ` "default" `
641
- conditions are defined and implemented in Node.js core,
642
- [ as specified above ] ( #conditional-exports ) .
647
+ Condition strings other than the ` "import" ` , ` "require" ` , ` "node" ` ,
648
+ ` "node-addons" ` and ` "default" ` conditions
649
+ [ implemented in Node.js core ] ( #conditional-exports ) are ignored by default .
643
650
644
- The ` "node-addons" ` condition can be used to provide an entry point which
645
- uses native C++ addons. However, this condition can be disabled via the
646
- [ ` --no-addons ` flag] [ ] . When using ` "node-addons" ` , it's recommended to treat
647
- ` "default" ` as an enhancement that provides a more universal entry point, e.g.
648
- using WebAssembly instead of a native addon.
649
-
650
- Other condition strings are unknown to Node.js and thus ignored by default.
651
- Runtimes or tools other than Node.js can use them at their discretion.
651
+ Other platforms may implement other conditions and user conditions can be
652
+ enabled in Node.js via the [ ` --conditions ` / ` -C ` flag] [ ] .
652
653
653
- These user conditions can be enabled in Node.js via the [ ` --conditions ` flag] [ ] .
654
+ Since custom package conditions require clear definitions to ensure correct
655
+ usage, a list of common known package conditions and their strict definitions
656
+ is provided below to assist with ecosystem coordination.
654
657
655
- The following condition definitions are currently endorsed by Node.js:
656
-
657
- * ` "browser" ` - any environment which implements a standard subset of global
658
- browser APIs available from JavaScript in web browsers, including the DOM
659
- APIs.
658
+ * ` "types" ` - can be used by typing systems to resolve the typing file for
659
+ the given export. _ This condition should always be included first._
660
+ * ` "deno" ` - indicates a variation for the Deno platform.
661
+ * ` "browser" ` - any web browser environment.
660
662
* ` "development" ` - can be used to define a development-only environment
661
- entry point. _ Must always be mutually exclusive with ` "production" ` ._
663
+ entry point, for example to provide additional debugging context such as
664
+ better error messages when running in a development mode. _ Must always be
665
+ mutually exclusive with ` "production" ` ._
662
666
* ` "production" ` - can be used to define a production environment entry
663
667
point. _ Must always be mutually exclusive with ` "development" ` ._
664
668
665
- The above user conditions can be enabled in Node.js via the
666
- [ ` --conditions ` flag] [ ] .
667
-
668
- Platform specific conditions such as ` "deno" ` , ` "electron" ` , or ` "react-native" `
669
- may be used, but while there remain no implementation or integration intent
670
- from these platforms, the above are not explicitly endorsed by Node.js.
671
-
672
669
New conditions definitions may be added to this list by creating a pull request
673
670
to the [ Node.js documentation for this section] [ ] . The requirements for listing
674
671
a new condition definition here are that:
@@ -1278,7 +1275,7 @@ This field defines [subpath imports][] for the current package.
1278
1275
[ `"name"` ] : #name
1279
1276
[ `"packageManager"` ] : #packagemanager
1280
1277
[ `"type"` ] : #type
1281
- [ `--conditions` flag ] : #resolving-user-conditions
1278
+ [ `--conditions` / `-C` flag ] : #resolving-user-conditions
1282
1279
[ `--no-addons` flag ] : cli.md#--no-addons
1283
1280
[ `ERR_PACKAGE_PATH_NOT_EXPORTED` ] : errors.md#err_package_path_not_exported
1284
1281
[ `esm` ] : https://github.com/standard-things/esm#readme
0 commit comments