GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: explain fallback arrays in package exports targets · nodejs/node@e396cb0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • packages.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/packages.md‎

Copy file name to clipboardExpand all lines: doc/api/packages.md
+36Lines changed: 36 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,40 @@ a nested condition does not have any mapping it will continue checking
782782
the remaining conditions of the parent condition. In this way nested
783783
conditions behave analogously to nested JavaScript `if` statements.
784784

785+
### Target fallback arrays
786+
787+
Anywhere a target is accepted in the [`"exports"`][] or [`"imports"`][] fields,
788+
an array of targets can be used instead. Node.js tries the items in order and
789+
uses the first one that is a valid target. An item is skipped, and the next one
790+
is tried, only when:
791+
792+
* it is not a valid target, for example a string that does not follow the
793+
[path rules for export targets][], or
794+
* it is a conditions object in which no condition matches.
795+
796+
Fallback arrays exist for compatibility: a package can list a target that only
797+
some environments or future versions of Node.js treat as valid, followed by one
798+
that all of them support.
799+
800+
Node.js does not check whether the file a target points to exists. Once an
801+
item is a valid target, it is used and the remaining items are ignored, even if
802+
loading the file then fails because it is missing:
803+
804+
```json
805+
{
806+
"exports": {
807+
"./feature": ["./src/feature.js", "./dist/feature.js"]
808+
}
809+
}
810+
```
811+
812+
Here `./feature` always resolves to `./src/feature.js`. An array cannot be used
813+
to fall back from a missing file to another file.
814+
815+
If the array is empty, the subpath is not exported. If every item is an
816+
invalid target, the error for the last item is thrown. The exact behavior is
817+
defined by `PACKAGE_TARGET_RESOLVE` in the [package resolution algorithm][].
818+
785819
### Resolving user conditions
786820

787821
<!-- YAML
@@ -1417,7 +1451,9 @@ Node.js allows type stripping for:
14171451
[import maps]: https://github.com/WICG/import-maps
14181452
[load ECMAScript modules from CommonJS modules]: modules.md#loading-ecmascript-modules-using-require
14191453
[merve]: https://github.com/anonrig/merve
1454+
[package resolution algorithm]: esm.md#resolution-algorithm-specification
14201455
[packages folder mapping]: https://github.com/WICG/import-maps#packages-via-trailing-slashes
1456+
[path rules for export targets]: #path-rules-and-validation-for-export-targets
14211457
[resolution algorithm pseudo-code]: modules.md#all-together
14221458
[self-reference]: #self-referencing-a-package-using-its-name
14231459
[subpath exports]: #subpath-exports

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.