Skip to content

Commit e31d1cb

Browse files
committed
doc: define "types", "deno" community conditions
PR-URL: #40708 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent cedcc16 commit e31d1cb

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

doc/api/packages.md

+33-36
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,17 @@ For example, a package that wants to provide different ES module exports for
486486
}
487487
```
488488

489-
Node.js implements the following conditions:
489+
Node.js implements the following conditions, listed in order from most
490+
specific to least specific as conditions should be defined:
490491

492+
* `"node-addons"` - similar to `"node"` and matches for any Node.js environment.
493+
This condition can be used to provide an entry point which uses native C++
494+
addons as opposed to an entry point which is more universal and doesn't rely
495+
on native addons. This condition can be disabled via the
496+
[`--no-addons` flag][].
497+
* `"node"` - matches for any Node.js environment. Can be a CommonJS or ES
498+
module file. _In most cases explicitly calling out the Node.js platform is
499+
not necessary._
491500
* `"import"` - matches when the package is loaded via `import` or
492501
`import()`, or via any top-level import or resolve operation by the
493502
ECMAScript module loader. Applies regardless of the module format of the
@@ -498,14 +507,6 @@ Node.js implements the following conditions:
498507
formats include CommonJS, JSON, and native addons but not ES modules as
499508
`require()` doesn't support them. _Always mutually exclusive with
500509
`"import"`._
501-
* `"node"` - matches for any Node.js environment. Can be a CommonJS or ES
502-
module file. _This condition should always come after `"import"` or
503-
`"require"`._
504-
* `"node-addons"` - similar to `"node"` and matches for any Node.js environment.
505-
This condition can be used to provide an entry point which uses native C++
506-
addons as opposed to an entry point which is more universal and doesn't rely
507-
on native addons. This condition can be disabled via the
508-
[`--no-addons` flag][].
509510
* `"default"` - the generic fallback that always matches. Can be a CommonJS
510511
or ES module file. _This condition should always come last._
511512

@@ -517,6 +518,12 @@ least specific in object order_.
517518
Using the `"import"` and `"require"` conditions can lead to some hazards,
518519
which are further explained in [the dual CommonJS/ES module packages section][].
519520

521+
The `"node-addons"` condition can be used to provide an entry point which
522+
uses native C++ addons. However, this condition can be disabled via the
523+
[`--no-addons` flag][]. When using `"node-addons"`, it's recommended to treat
524+
`"default"` as an enhancement that provides a more universal entry point, e.g.
525+
using WebAssembly instead of a native addon.
526+
520527
Conditional exports can also be extended to exports subpaths, for example:
521528

522529
```json
@@ -590,40 +597,30 @@ exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`,
590597

591598
Any number of custom conditions can be set with repeat flags.
592599

593-
### Conditions Definitions
600+
### Community Conditions Definitions
594601

595-
The `"import"`, `"require"`, `"node"`, `"node-addons"` and `"default"`
596-
conditions are defined and implemented in Node.js core,
597-
[as specified above](#conditional-exports).
602+
Condition strings other than the `"import"`, `"require"`, `"node"`,
603+
`"node-addons"` and `"default"` conditions
604+
[implemented in Node.js core](#conditional-exports) are ignored by default.
598605

599-
The `"node-addons"` condition can be used to provide an entry point which
600-
uses native C++ addons. However, this condition can be disabled via the
601-
[`--no-addons` flag][]. When using `"node-addons"`, it's recommended to treat
602-
`"default"` as an enhancement that provides a more universal entry point, e.g.
603-
using WebAssembly instead of a native addon.
604-
605-
Other condition strings are unknown to Node.js and thus ignored by default.
606-
Runtimes or tools other than Node.js can use them at their discretion.
606+
Other platforms may implement other conditions and user conditions can be
607+
enabled in Node.js via the [`--conditions` / `-C` flag][].
607608

608-
These user conditions can be enabled in Node.js via the [`--conditions` flag][].
609+
Since custom package conditions require clear definitions to ensure correct
610+
usage, a list of common known package conditions and their strict definitions
611+
is provided below to assist with ecosystem coordination.
609612

610-
The following condition definitions are currently endorsed by Node.js:
611-
612-
* `"browser"` - any environment which implements a standard subset of global
613-
browser APIs available from JavaScript in web browsers, including the DOM
614-
APIs.
613+
* `"types"` - can be used by typing systems to resolve the typing file for
614+
the given export. _This condition should always be included first._
615+
* `"deno"` - indicates a variation for the Deno platform.
616+
* `"browser"` - any web browser environment.
615617
* `"development"` - can be used to define a development-only environment
616-
entry point. _Must always be mutually exclusive with `"production"`._
618+
entry point, for example to provide additional debugging context such as
619+
better error messages when running in a development mode. _Must always be
620+
mutually exclusive with `"production"`._
617621
* `"production"` - can be used to define a production environment entry
618622
point. _Must always be mutually exclusive with `"development"`._
619623

620-
The above user conditions can be enabled in Node.js via the
621-
[`--conditions` flag][].
622-
623-
Platform specific conditions such as `"deno"`, `"electron"`, or `"react-native"`
624-
may be used, but while there remain no implementation or integration intent
625-
from these platforms, the above are not explicitly endorsed by Node.js.
626-
627624
New conditions definitions may be added to this list by creating a pull request
628625
to the [Node.js documentation for this section][]. The requirements for listing
629626
a new condition definition here are that:
@@ -1233,7 +1230,7 @@ This field defines [subpath imports][] for the current package.
12331230
[`"name"`]: #name
12341231
[`"packageManager"`]: #packagemanager
12351232
[`"type"`]: #type
1236-
[`--conditions` flag]: #resolving-user-conditions
1233+
[`--conditions` / `-C` flag]: #resolving-user-conditions
12371234
[`--no-addons` flag]: cli.md#--no-addons
12381235
[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported
12391236
[`esm`]: https://github.com/standard-things/esm#readme

0 commit comments

Comments
 (0)