Skip to content

doc: document exports target fallback arrays - #65240

Open
theSnackOverflow wants to merge 1 commit into
nodejs:mainfrom
theSnackOverflow:doc-exports-fallback-arrays
Open

doc: document exports target fallback arrays#65240
theSnackOverflow wants to merge 1 commit into
nodejs:mainfrom
theSnackOverflow:doc-exports-fallback-arrays

Conversation

@theSnackOverflow

Copy link
Copy Markdown
Contributor

"exports" accepts an array of targets, but packages.md never described it.
The only trace was the string[] in the field's type notation, which is what
the issue reports.

What this documents

A new "Target fallback arrays" section covering when Node.js skips an item and
continues with the next one:

  • Node.js does not recognize the target's syntax
  • the target is an object and none of its conditions match
  • the target is null

and, explicitly, that a missing file is not one of them. Targets are matched
without checking the filesystem, so an array of valid paths always resolves to
the first one. That is the misconception the issue author ran into: after
noticing paths are matched regardless of whether the file exists, they asked
"what is the practical purpose of the array?" and had to find the answer outside
the Node.js documentation.

Verification

The behaviour was read off resolvePackageTarget() in
lib/internal/modules/esm/resolve.js and cross-checked against the existing
fixtures, which encode every skip case:

"./fallbackfile": [[], null, {}, "builtin:x", "./asdf.js"],
"./nofallback1": [],
"./nofallback2": [null, {}, "builtin:x"]

test/es-module/test-esm-exports.mjs expects nofallback1 to behave as not
exported and nofallback2 to be an invalid target, which is the distinction the
final paragraph of the new section draws.

  • node tools/lint-md/lint-md.mjs doc/api/packages.md — clean
  • python3 tools/test.py doctool — 3/3 passing

Relation to earlier work

This supersedes #63340, which was closed without landing. That PR received a
review comment from @ljharb pointing out that fallback also happens when a
Node.js version cannot understand a target's syntax, so an older version skips
an object target and falls through. The comment was never addressed. It is now
the first bullet of the new section, because it is what makes fallback arrays
useful for compatibility rather than a file-existence fallback.

@Prakhar2275 expressed interest in documenting this in the issue thread but did
not open a PR. Happy to step aside if they are still working on it.

The `"exports"` field accepts an array of targets, but the only trace of
it in the documentation was the `string[]` in the field's type notation.
Nothing described when Node.js moves on to the next item.

Document the three cases that skip an item: unrecognized target syntax,
an object whose conditions do not match, and `null`. Also state that a
missing file is not one of them, since targets are matched without
checking the filesystem, so an array of valid paths always resolves to
the first one.

The syntax case comes from review feedback left on an earlier attempt
that was closed without landing: a version that predates a given target
form treats that form as invalid and falls through, which is what makes
these arrays useful for compatibility.

Fixes: nodejs#58600
Refs: nodejs#63340
Signed-off-by: Ji Hoon Kang <ivory.ma9ic@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Issues and PRs related to the documentations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants