Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7be8dde

Browse filesBrowse files
guybedfordaddaleax
authored andcommitted
doc: clarify conditional exports guidance
PR-URL: #34306 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com>
1 parent c1b5c89 commit 7be8dde
Copy full SHA for 7be8dde

File tree

Expand file treeCollapse file tree

1 file changed

+13
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+13
-5
lines changed
Open diff view settings
Collapse file

‎doc/api/esm.md‎

Copy file name to clipboardExpand all lines: doc/api/esm.md
+13-5Lines changed: 13 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,24 @@ Conditional exports can also be extended to exports subpaths, for example:
456456
"exports": {
457457
".": "./main.js",
458458
"./feature": {
459-
"browser": "./feature-browser.js",
459+
"node": "./feature-node.js",
460460
"default": "./feature.js"
461461
}
462462
}
463463
}
464464
```
465465

466466
Defines a package where `require('pkg/feature')` and `import 'pkg/feature'`
467-
could provide different implementations between the browser and Node.js,
468-
given third-party tool support for a `"browser"` condition.
467+
could provide different implementations between Node.js and other JS
468+
environments.
469+
470+
When using environment branches, always include a `"default"` condition where
471+
possible. Providing a `"default"` condition ensures that any unknown JS
472+
environments are able to use this universal implementation, which helps avoid
473+
these JS environments from having to pretend to be existing environments in
474+
order to support packages with conditional exports. For this reason, using
475+
`"node"` and `"default"` condition branches is usually preferable to using
476+
`"node"` and `"browser"` condition branches.
469477

470478
#### Nested conditions
471479

@@ -479,11 +487,11 @@ use in Node.js but not the browser:
479487
{
480488
"main": "./main.js",
481489
"exports": {
482-
"browser": "./feature-browser.mjs",
483490
"node": {
484491
"import": "./feature-node.mjs",
485492
"require": "./feature-node.cjs"
486-
}
493+
},
494+
"default": "./feature.mjs",
487495
}
488496
}
489497
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.