Skip to content

Commit 05f34c6

Browse files
guybedforddanielleadams
authored andcommitted
doc: define "browser", "production", "development"
Defines the above exports conditions by convention as well as some guidance for further conditions definitions in future PR-URL: #36856 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 1db3772 commit 05f34c6

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

doc/api/packages.md

+48-6
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ For example, a package that wants to provide different ES module exports for
446446
}
447447
```
448448

449-
Node.js supports the following conditions out of the box:
449+
Node.js implements the following conditions:
450450

451451
* `"import"` - matches when the package is loaded via `import` or
452452
`import()`, or via any top-level import or resolve operation by the
@@ -469,11 +469,6 @@ matching, earlier entries have higher priority and take precedence over later
469469
entries. _The general rule is that conditions should be from most specific to
470470
least specific in object order_.
471471

472-
Other conditions such as `"browser"`, `"electron"`, `"deno"`, `"react-native"`,
473-
etc., are unknown to Node.js, and thus ignored. Runtimes or tools other than
474-
Node.js can use them at their discretion. Further restrictions, definitions, or
475-
guidance on condition names might occur in the future.
476-
477472
Using the `"import"` and `"require"` conditions can lead to some hazards,
478473
which are further explained in [the dual CommonJS/ES module packages section][].
479474

@@ -544,6 +539,52 @@ exports, while resolving the existing `"node"`, `"default"`, `"import"`, and
544539

545540
Any number of custom conditions can be set with repeat flags.
546541

542+
### Conditions Definitions
543+
544+
The `"import"`, `"require"`, `"node"` and `"default"` conditions are defined
545+
and implemented in Node.js core,
546+
[as specified above](#esm_conditional_exports).
547+
548+
Other condition strings are unknown to Node.js and thus ignored by default.
549+
Runtimes or tools other than Node.js can use them at their discretion.
550+
551+
These user conditions can be enabled in Node.js via the [`--conditions`
552+
flag](#packages_resolving_user_conditions).
553+
554+
The following condition definitions are currently endorsed by Node.js:
555+
556+
* `"browser"` - any environment which implements a standard subset of global
557+
browser APIs available from JavaScript in web browsers, including the DOM
558+
APIs.
559+
* `"development"` - can be used to define a development-only environment
560+
entry point. _Must always be mutually exclusive with `"production"`._
561+
* `"production"` - can be used to define a production environment entry
562+
point. _Must always be mutually exclusive with `"development"`._
563+
564+
The above user conditions can be enabled in Node.js via the [`--conditions`
565+
flag](#packages_resolving_user_conditions).
566+
567+
Platform specific conditions such as `"deno"`, `"electron"`, or `"react-native"`
568+
may be used, but while there remain no implementation or integration intent
569+
from these platforms, the above are not explicitly endorsed by Node.js.
570+
571+
New conditions definitions may be added to this list by creating a PR to the
572+
[Node.js documentation for this section][]. The requirements for listing a
573+
new condition definition here are that:
574+
575+
* The definition should be clear and unambigious for all implementers.
576+
* The use case for why the condition is needed should be clearly justified.
577+
* There should exist sufficient existing implementation usage.
578+
* The condition name should not conflict with another condition definition or
579+
condition in wide usage.
580+
* The listing of the condition definition should provide a coordination
581+
benefit to the ecosystem that wouldn't otherwise be possible. For example,
582+
this would not necessarily be the case for company-specific or
583+
application-specific conditions.
584+
585+
The above definitions may be moved to a dedicated conditions registry in due
586+
course.
587+
547588
### Self-referencing a package using its name
548589

549590
Within a package, the values defined in the package’s
@@ -1053,6 +1094,7 @@ This field defines [subpath imports][] for the current package.
10531094
[CommonJS]: modules.md
10541095
[ES module]: esm.md
10551096
[ES modules]: esm.md
1097+
[Node.js documentation for this section]: https://github.com/nodejs/node/blob/master/doc/api/packages.md#conditions-definitions
10561098
[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#errors_err_package_path_not_exported
10571099
[`esm`]: https://github.com/standard-things/esm#readme
10581100
[`"exports"`]: #packages_exports

0 commit comments

Comments
 (0)