Skip to content

module: show user location for missing module errors, resolve #38892#62885

Open
meixg wants to merge 1 commit intonodejs:mainfrom
meixg:issue-38892-module-not-found-location
Open

module: show user location for missing module errors, resolve #38892#62885
meixg wants to merge 1 commit intonodejs:mainfrom
meixg:issue-38892-module-not-found-location

Conversation

@meixg
Copy link
Copy Markdown
Member

@meixg meixg commented Apr 22, 2026

Fixes: #38892

Summary

This improves the source location shown for uncaught missing module errors.

Previously, some missing module failures displayed an internal loader location at the top of the fatal exception output, such as node:internal/modules/..., instead of pointing at the user import, dynamic import(), or require()
expression that triggered the resolution failure.

This change decorates missing module errors with an arrow message that points to the user source location.

Covered cases:

  • static ESM import
  • dynamic import()
  • CommonJS require()

Implementation

For static ESM imports, ModuleWrap#getModuleRequests() now exposes the line and column for each module request. The ESM linking path uses that information when decorating ERR_MODULE_NOT_FOUND.

For dynamic import(), the loader temporarily increases Error.stackTraceLimit around the synchronous getOrCreateModuleJob() call so the original ERR_MODULE_NOT_FOUND stack can retain the user import frame. The decorator then uses
that frame to locate the dynamic import expression.

For CommonJS, MODULE_NOT_FOUND decoration uses the current structured stack to find the parent module call site, avoiding parsing the formatted err.stack string.

The source snippet formatting is centralized in internal/errors/error_source.js, including clipping for long source lines.

Example

Before:

node:internal/modules/cjs/loader:...
  throw err;
  ^

Error: Cannot find module 'this-package-does-not-exist'

After:

/path/to/entry.cjs:2
const missing = require("this-package-does-not-exist");
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error: Cannot find module 'this-package-does-not-exist'

Tests

Added coverage for:

  • static ESM import missing package errors
  • dynamic import missing package errors from both .mjs and .cjs
  • CommonJS require missing package errors
  • long-line source snippet clipping

Relevant tests:

  • test/es-module/test-esm-module-not-found-error-location.mjs
  • test/es-module/test-esm-dynamic-import-not-found-error-location.mjs
  • test/parallel/test-module-not-found-error-location.js

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Apr 22, 2026
@meixg meixg force-pushed the issue-38892-module-not-found-location branch from d24ea56 to a9aeca0 Compare April 22, 2026 11:16
@meixg meixg force-pushed the issue-38892-module-not-found-location branch from a9aeca0 to 2142429 Compare April 22, 2026 12:21
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 92.70517% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.64%. Comparing base (53b8d3c) to head (2142429).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/esm/module_job.js 86.07% 11 Missing ⚠️
lib/internal/modules/esm/loader.js 93.93% 5 Missing and 1 partial ⚠️
lib/internal/errors/error_source.js 95.00% 4 Missing ⚠️
lib/internal/modules/cjs/loader.js 95.31% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62885      +/-   ##
==========================================
+ Coverage   89.62%   89.64%   +0.01%     
==========================================
  Files         706      706              
  Lines      219137   219492     +355     
  Branches    41981    42065      +84     
==========================================
+ Hits       196404   196763     +359     
- Misses      14614    14617       +3     
+ Partials     8119     8112       -7     
Files with missing lines Coverage Δ
src/module_wrap.cc 74.56% <100.00%> (+0.14%) ⬆️
lib/internal/modules/cjs/loader.js 98.06% <95.31%> (-0.09%) ⬇️
lib/internal/errors/error_source.js 86.12% <95.00%> (+4.30%) ⬆️
lib/internal/modules/esm/loader.js 99.15% <93.93%> (-0.54%) ⬇️
lib/internal/modules/esm/module_job.js 95.07% <86.07%> (-1.33%) ⬇️

... and 38 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the error message for missing modules

2 participants