Skip to content

Conversation

@samarthsinh2660
Copy link
Contributor

Adds a check before module execution to provide a helpful error message when a module ID is not found in __webpack_modules__, instead of the cryptic Cannot read properties of undefined (reading 'call') error.

Fixes #18021

Summary

When a module ID is missing from __webpack_modules__, the webpack runtime currently tries to execute it and crashes with:

TypeError: Cannot read properties of undefined (reading 'call')

This error originates from the generated runtime in JavascriptModulesPlugin and does not clearly tell users that the actual problem is a missing/unloaded module.

This PR adds a defensive check before executing the module factory:

  • If __webpack_modules__[moduleId] is undefined, we now throw a descriptive Error explaining that the module with the given ID cannot be found and may not be loaded or was removed.
  • This turns a low-level TypeError into a clear, actionable error message, improving the UX when module loading goes wrong (e.g. dynamic imports, federation, or other advanced setups).

What kind of change does this PR introduce?

  • Bugfix / UX improvement (better runtime error handling)
  • New feature
  • Refactoring
  • Build related change
  • Other

Did you add tests for your changes?

  • Yes
  • No

This change is a small defensive check inside the runtime path and is mainly focused on improving the error message, not changing the control flow. The existing runtime/missing-module tests already cover the behavior that an Error is thrown when a module is missing; this PR only makes the message clearer.

I am happy to add a dedicated test if maintainers can suggest the preferred place and pattern (e.g. extending test/cases/runtime/error-handling or one of the missing-module-* runtime cases) to assert the new message.

Does this PR introduce a breaking change?

  • No
  • Yes

The behavior (throwing an error when a module is missing) stays the same; only the error message becomes more descriptive.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

No documentation updates are required. This change only improves the runtime error message surfaced to users when a module cannot be found.

@changeset-bot
Copy link

changeset-bot bot commented Dec 10, 2025

🦋 Changeset detected

Latest commit: 186250b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Template.indent([
"throw new Error('Cannot find module with id \"' + moduleId + '\". The module might not be loaded or was removed.');"
]),
"}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be only for development mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Development-only error:
Wrapped the helpful error in a check using outputOptions.pathinfo, which is only true in development (see lib/config/defaults.js, where output.output.pathinfo is set based on development). So the error is thrown only in dev builds.

return `__webpack_modules__[${moduleIdExpr}](${args.join(",")});`;
return `__webpack_modules__[${moduleIdExpr}](${args.join(
","
)});`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unrelated changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The webpack_modules${moduleIdExpr}; calls are unchanged now. The only change in
JavascriptModulesPlugin.js
is the new development-only check right after the cache lookup and before creating the module.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add a test case

"// Check if module exists (development only)",
"if (__webpack_modules__[moduleId] === undefined) {",
Template.indent([
"throw new Error('Cannot find module with id \"' + moduleId + '\". The module might not be loaded or was removed.');"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good — we can make the error message fully match Node.js and set the error code to MODULE_NOT_FOUND.
telegram-cloud-photo-size-5-6183626581591395300-y

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated! The error now matches Node.js format:

Message: Cannot find module ''
Error code: MODULE_NOT_FOUND
Also rebased on latest main.

Adds a check before module execution to provide a helpful error message when a module ID is not found in __webpack_modules__, instead of the cryptic 'Cannot read properties of undefined (reading call)' error.

Fixes webpack#18021
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 11, 2025

CodSpeed Performance Report

Merging #20221 will not alter performance

Comparing samarthsinh2660:fix/improve-module-not-found-error (186250b) with main (0c52a8f)

Summary

✅ 72 untouched

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, let's fix tests and we can merge, thanks

@samarthsinh2660 samarthsinh2660 force-pushed the fix/improve-module-not-found-error branch from 57b424b to dd198d7 Compare December 11, 2025 14:45
@alexander-akait
Copy link
Member

Please update snapshots for cached too

@samarthsinh2660 samarthsinh2660 force-pushed the fix/improve-module-not-found-error branch from dd198d7 to 4455b9f Compare December 11, 2025 16:01
@alexander-akait
Copy link
Member

Just run yarn test:base ./test/ConfigTestCases.basictest.js -u --no-verbose and yarn test:base ./test/ConfigCacheTestCases.longtest.js -u --no-verbose

@samarthsinh2660 samarthsinh2660 force-pushed the fix/improve-module-not-found-error branch from 4455b9f to ac878f2 Compare December 11, 2025 16:29
@alexander-akait alexander-akait merged commit 22c48fb into webpack:main Dec 11, 2025
90 of 91 checks passed
@alexander-akait
Copy link
Member

Thanks

@github-actions
Copy link
Contributor

This PR is packaged and the instant preview is available (22c48fb).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@22c48fb
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@22c48fb
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@22c48fb

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add null check to avoid TypeError: Cannot read properties of undefined (reading 'call')

3 participants