@@ -531,8 +531,17 @@ For example, a package that wants to provide different ES module exports for
531531}
532532```
533533
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:
535536
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._
536545* ` "import" ` - matches when the package is loaded via ` import ` or
537546 ` import() ` , or via any top-level import or resolve operation by the
538547 ECMAScript module loader. Applies regardless of the module format of the
@@ -543,14 +552,6 @@ Node.js implements the following conditions:
543552 formats include CommonJS, JSON, and native addons but not ES modules as
544553 ` require() ` doesn't support them. _ Always mutually exclusive with
545554 ` "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] [ ] .
554555* ` "default" ` - the generic fallback that always matches. Can be a CommonJS
555556 or ES module file. _ This condition should always come last._
556557
@@ -562,6 +563,12 @@ least specific in object order_.
562563Using the ` "import" ` and ` "require" ` conditions can lead to some hazards,
563564which are further explained in [ the dual CommonJS/ES module packages section] [ ] .
564565
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+
565572Conditional exports can also be extended to exports subpaths, for example:
566573
567574``` json
@@ -635,40 +642,30 @@ exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`,
635642
636643Any number of custom conditions can be set with repeat flags.
637644
638- ### Conditions Definitions
645+ ### Community Conditions Definitions
639646
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 .
643650
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] [ ] .
652653
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.
654657
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.
660662* ` "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" ` ._
662666* ` "production" ` - can be used to define a production environment entry
663667 point. _ Must always be mutually exclusive with ` "development" ` ._
664668
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-
672669New conditions definitions may be added to this list by creating a pull request
673670to the [ Node.js documentation for this section] [ ] . The requirements for listing
674671a new condition definition here are that:
@@ -1278,7 +1275,7 @@ This field defines [subpath imports][] for the current package.
12781275[ `"name"` ] : #name
12791276[ `"packageManager"` ] : #packagemanager
12801277[ `"type"` ] : #type
1281- [ `--conditions` flag ] : #resolving-user-conditions
1278+ [ `--conditions` / `-C` flag ] : #resolving-user-conditions
12821279[ `--no-addons` flag ] : cli.md#--no-addons
12831280[ `ERR_PACKAGE_PATH_NOT_EXPORTED` ] : errors.md#err_package_path_not_exported
12841281[ `esm` ] : https://github.com/standard-things/esm#readme
0 commit comments