| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent de4d018 commit 694012b
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -486,8 +486,17 @@ For example, a package that wants to provide different ES module exports for | |||
| 486 | 486 | } | |
| 487 | 487 | ``` | |
| 488 | 488 | ||
| 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: | ||
| 490 | 491 | ||
| 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._ | ||
| 491 | 500 | * `"import"` - matches when the package is loaded via `import` or | |
| 492 | 501 | `import()`, or via any top-level import or resolve operation by the | |
| 493 | 502 | ECMAScript module loader. Applies regardless of the module format of the | |
@@ -498,14 +507,6 @@ Node.js implements the following conditions: | |||
| 498 | 507 | formats include CommonJS, JSON, and native addons but not ES modules as | |
| 499 | 508 | `require()` doesn't support them. _Always mutually exclusive with | |
| 500 | 509 | `"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][]. | ||
| 509 | 510 | * `"default"` - the generic fallback that always matches. Can be a CommonJS | |
| 510 | 511 | or ES module file. _This condition should always come last._ | |
| 511 | 512 | ||
@@ -517,6 +518,12 @@ least specific in object order_. | |||
| 517 | 518 | Using the `"import"` and `"require"` conditions can lead to some hazards, | |
| 518 | 519 | which are further explained in [the dual CommonJS/ES module packages section][]. | |
| 519 | 520 | ||
| 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 | + | ||
| 520 | 527 | Conditional exports can also be extended to exports subpaths, for example: | |
| 521 | 528 | ||
| 522 | 529 | ```json | |
@@ -590,40 +597,30 @@ exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`, | |||
| 590 | 597 | ||
| 591 | 598 | Any number of custom conditions can be set with repeat flags. | |
| 592 | 599 | ||
| 593 | - ### Conditions Definitions | ||
| 600 | + ### Community Conditions Definitions | ||
| 594 | 601 | ||
| 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. | ||
| 598 | 605 | ||
| 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][]. | ||
| 607 | 608 | ||
| 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. | ||
| 609 | 612 | ||
| 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. | ||
| 615 | 617 | * `"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"`._ | ||
| 617 | 621 | * `"production"` - can be used to define a production environment entry | |
| 618 | 622 | point. _Must always be mutually exclusive with `"development"`._ | |
| 619 | 623 | ||
| 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 | - | ||
| 627 | 624 | New conditions definitions may be added to this list by creating a pull request | |
| 628 | 625 | to the [Node.js documentation for this section][]. The requirements for listing | |
| 629 | 626 | a new condition definition here are that: | |
@@ -1233,7 +1230,7 @@ This field defines [subpath imports][] for the current package. | |||
| 1233 | 1230 | [`"name"`]: #name | |
| 1234 | 1231 | [`"packageManager"`]: #packagemanager | |
| 1235 | 1232 | [`"type"`]: #type | |
| 1236 | - [`--conditions` flag]: #resolving-user-conditions | ||
| 1233 | + [`--conditions` / `-C` flag]: #resolving-user-conditions | ||
| 1237 | 1234 | [`--no-addons` flag]: cli.md#--no-addons | |
| 1238 | 1235 | [`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported | |
| 1239 | 1236 | [`esm`]: https://github.com/standard-things/esm#readme | |
| Back | FazBrowse Home | New Git URL |
0 commit comments