Skip to content

module: fix wrong error annotation for require of ESM#62685

Open
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/55350-wrong-error-annotation-require-esm
Open

module: fix wrong error annotation for require of ESM#62685
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/55350-wrong-error-annotation-require-esm

Conversation

@om-ghante
Copy link
Copy Markdown

When a CommonJS module requires an ES module with
--no-experimental-require-module, the error annotation (arrow message)
points to an internal frame instead of the user's actual require() call.

Before (broken)

The error annotation incorrectly points to node:diagnostics_channel:315
with content undefined, because TracingChannel.traceSync is the first
frame in the stack trace.

After (fixed)

The error annotation correctly points to the user's file and line where
require() was called, showing the actual source line.

Root Cause

The reconstructErrorStack function always picked the first at frame
from the error stack trace. Since v22.4.0, require() is wrapped by
TracingChannel.traceSync via wrapModuleLoad, so the first frame is
the internal tracing wrapper instead of the user's code.

Fix

Search the stack frames for one matching the parent module's file path,
instead of blindly using the first frame. This also addresses an
existing TODO comment about trimming internal frames.

Fixes: #55350

When a CommonJS module requires an ES module with
--no-experimental-require-module, the error annotation (arrow message)
was pointing to an internal frame (TracingChannel.traceSync in
node:diagnostics_channel) instead of the user's actual require() call.

This happened because reconstructErrorStack() was always picking the
first 'at' frame from the error stack trace. When the require() call
is wrapped by TracingChannel.traceSync (added in v22.4.0 via
wrapModuleLoad), the first frame is the internal tracing wrapper, not
the user's code.

Fix reconstructErrorStack() to search the stack frames for one that
matches the parent module's file path, instead of blindly using the
first frame. This ensures the error annotation correctly points to the
user's require() call.

Also remove a TODO comment that this change addresses.

Fixes: nodejs#55350
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong error annotation when commonjs requires an ES module

2 participants