Skip to content

Commit 45be6af

Browse files
author
RoomWithOutRoof
committed
doc: add guidance on using node/default conditions for dual packages
As an alternative to using "import"/"require" conditions, packages can use "node"/"default" conditions to avoid the dual package hazard while still providing ES modules for non-Node.js environments. This addresses issue #52174.
1 parent d0fa608 commit 45be6af

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

doc/api/packages.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,24 @@ least specific in object order_.
720720

721721
Using the `"import"` and `"require"` conditions can lead to some hazards,
722722
which are further explained in [the dual CommonJS/ES module packages section][].
723+
As an alternative to using `"import"` and `"require"`, packages can use
724+
`"node"` and `"default"` conditions to avoid the dual package hazard while
725+
still providing ES modules for non-Node.js environments.
726+
727+
For example, using `"node"` and `"default"` instead of `"require"` and `"import"`:
728+
729+
```json
730+
{
731+
"exports": {
732+
"node": "./index.cjs",
733+
"default": "./index.mjs"
734+
}
735+
}
736+
```
737+
738+
This approach avoids the dual package hazard in Node.js (since `"node"` always
739+
matches for any Node.js environment) while still allowing bundlers to use the
740+
ES module version when targeting other JavaScript runtimes.
723741

724742
The `"node-addons"` condition can be used to provide an entry point which
725743
uses native C++ addons. However, this condition can be disabled via the

0 commit comments

Comments
 (0)