module: add requireStack to ESM exports resolution errors#62417
Open
AKXtreme wants to merge 1 commit intonodejs:mainfrom
Open
module: add requireStack to ESM exports resolution errors#62417AKXtreme wants to merge 1 commit intonodejs:mainfrom
AKXtreme wants to merge 1 commit intonodejs:mainfrom
Conversation
When require() fails via the ESM exports resolution path (e.g.
require('pkg/subpath') where pkg has a package.json exports field),
the thrown MODULE_NOT_FOUND error was missing the requireStack
property and the "Require stack:" section in the message that the
CJS resolution path has always included.
Thread the parent Module through createEsmNotFoundErr,
finalizeEsmResolution, resolveExports, Module._findPath, and
trySelf so all ESM resolution code paths populate requireStack
consistently with the CJS path.
Fixes TODO(BridgeAR) in lib/internal/modules/cjs/loader.js
Collaborator
|
Review requested:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
require()fails via the ESM exports resolution path (e.g.require('pkg/subpath')wherepkghas apackage.jsonexportsfield), the thrownMODULE_NOT_FOUNDerror was missing therequireStackproperty and the"Require stack:"section in the message — even though the CJS resolution path has included this information for years.This was acknowledged in a
TODO(BridgeAR)comment inlib/internal/modules/cjs/loader.js.Changes
createEsmNotFoundErr— accepts an optionalparentModule and buildsrequireStack+ message using the same pattern as the CJS path. Removes the TODO comment.finalizeEsmResolution— threadsparentthrough tocreateEsmNotFoundErr.resolveExports— threadsparentthrough. Covers the most common case:require('pkg/subpath')with anexportsfield.Module._findPath— accepts optional 5th argparent(fully backward-compatible) and passes it toresolveExports.trySelf— threadsparentthrough. Covers self-referencing packages.Module._resolveFilename— passesparentat all three ESM call sites.Before
After
Test plan
test/fixtures/node_modules/pkgexports-esm-require-stack/— a fixture package whoseexportsmap points to a non-existent file.test/parallel/test-require-esm-not-found-require-stack.jswith two scenarios:err.requireStackis present anderr.messageincludes"Require stack:".requireStackin the correct order (nearest caller first).