Skip to content

Commit 22c48fb

Browse files
fix: added check for module existence with helpful error message (#20221)
1 parent 0c52a8f commit 22c48fb

File tree

7 files changed

+115
-0
lines changed

7 files changed

+115
-0
lines changed

.changeset/cuddly-cameras-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Added module existence check for informative error message in development mode.

lib/javascript/JavascriptModulesPlugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,19 @@ class JavascriptModulesPlugin {
15611561
])
15621562
: Template.indent("return cachedModule.exports;"),
15631563
"}",
1564+
// Add helpful error message in development mode when module is not found
1565+
...(outputOptions.pathinfo
1566+
? [
1567+
"// Check if module exists (development only)",
1568+
"if (__webpack_modules__[moduleId] === undefined) {",
1569+
Template.indent([
1570+
'var e = new Error("Cannot find module \'" + moduleId + "\'");',
1571+
"e.code = 'MODULE_NOT_FOUND';",
1572+
"throw e;"
1573+
]),
1574+
"}"
1575+
]
1576+
: []),
15641577
"// Create a new module (and put it into the cache)",
15651578
"var module = __webpack_module_cache__[moduleId] = {",
15661579
Template.indent([

test/__snapshots__/ConfigCacheTestCases.longtest.js.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15853,6 +15853,12 @@ console.log({ HomeLayout });
1585315853
/******/ if (cachedModule !== undefined) {
1585415854
/******/ return cachedModule.exports;
1585515855
/******/ }
15856+
/******/ // Check if module exists (development only)
15857+
/******/ if (__webpack_modules__[moduleId] === undefined) {
15858+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
15859+
/******/ e.code = 'MODULE_NOT_FOUND';
15860+
/******/ throw e;
15861+
/******/ }
1585615862
/******/ // Create a new module (and put it into the cache)
1585715863
/******/ var module = __webpack_module_cache__[moduleId] = {
1585815864
/******/ // no module.id needed
@@ -16052,6 +16058,12 @@ console.log({ HomeLayout });
1605216058
/******/ if (cachedModule !== undefined) {
1605316059
/******/ return cachedModule.exports;
1605416060
/******/ }
16061+
/******/ // Check if module exists (development only)
16062+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16063+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16064+
/******/ e.code = 'MODULE_NOT_FOUND';
16065+
/******/ throw e;
16066+
/******/ }
1605516067
/******/ // Create a new module (and put it into the cache)
1605616068
/******/ var module = __webpack_module_cache__[moduleId] = {
1605716069
/******/ // no module.id needed
@@ -16251,6 +16263,12 @@ console.log({ HomeLayout });
1625116263
/******/ if (cachedModule !== undefined) {
1625216264
/******/ return cachedModule.exports;
1625316265
/******/ }
16266+
/******/ // Check if module exists (development only)
16267+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16268+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16269+
/******/ e.code = 'MODULE_NOT_FOUND';
16270+
/******/ throw e;
16271+
/******/ }
1625416272
/******/ // Create a new module (and put it into the cache)
1625516273
/******/ var module = __webpack_module_cache__[moduleId] = {
1625616274
/******/ // no module.id needed
@@ -16450,6 +16468,12 @@ console.log({ HomeLayout });
1645016468
/******/ if (cachedModule !== undefined) {
1645116469
/******/ return cachedModule.exports;
1645216470
/******/ }
16471+
/******/ // Check if module exists (development only)
16472+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16473+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16474+
/******/ e.code = 'MODULE_NOT_FOUND';
16475+
/******/ throw e;
16476+
/******/ }
1645316477
/******/ // Create a new module (and put it into the cache)
1645416478
/******/ var module = __webpack_module_cache__[moduleId] = {
1645516479
/******/ // no module.id needed
@@ -16614,6 +16638,12 @@ __WEBPACK_EXTERNAL_MODULE_externals1_v__;
1661416638
/******/ if (cachedModule !== undefined) {
1661516639
/******/ return cachedModule.exports;
1661616640
/******/ }
16641+
/******/ // Check if module exists (development only)
16642+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16643+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16644+
/******/ e.code = 'MODULE_NOT_FOUND';
16645+
/******/ throw e;
16646+
/******/ }
1661716647
/******/ // Create a new module (and put it into the cache)
1661816648
/******/ var module = __webpack_module_cache__[moduleId] = {
1661916649
/******/ // no module.id needed
@@ -16721,6 +16751,12 @@ __WEBPACK_EXTERNAL_MODULE_externals1_v__;
1672116751
/******/ if (cachedModule !== undefined) {
1672216752
/******/ return cachedModule.exports;
1672316753
/******/ }
16754+
/******/ // Check if module exists (development only)
16755+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16756+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16757+
/******/ e.code = 'MODULE_NOT_FOUND';
16758+
/******/ throw e;
16759+
/******/ }
1672416760
/******/ // Create a new module (and put it into the cache)
1672516761
/******/ var module = __webpack_module_cache__[moduleId] = {
1672616762
/******/ // no module.id needed
@@ -16828,6 +16864,12 @@ __WEBPACK_EXTERNAL_MODULE_externals1_v__;
1682816864
/******/ if (cachedModule !== undefined) {
1682916865
/******/ return cachedModule.exports;
1683016866
/******/ }
16867+
/******/ // Check if module exists (development only)
16868+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16869+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16870+
/******/ e.code = 'MODULE_NOT_FOUND';
16871+
/******/ throw e;
16872+
/******/ }
1683116873
/******/ // Create a new module (and put it into the cache)
1683216874
/******/ var module = __webpack_module_cache__[moduleId] = {
1683316875
/******/ // no module.id needed
@@ -16935,6 +16977,12 @@ __WEBPACK_EXTERNAL_MODULE_externals1_v__;
1693516977
/******/ if (cachedModule !== undefined) {
1693616978
/******/ return cachedModule.exports;
1693716979
/******/ }
16980+
/******/ // Check if module exists (development only)
16981+
/******/ if (__webpack_modules__[moduleId] === undefined) {
16982+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
16983+
/******/ e.code = 'MODULE_NOT_FOUND';
16984+
/******/ throw e;
16985+
/******/ }
1693816986
/******/ // Create a new module (and put it into the cache)
1693916987
/******/ var module = __webpack_module_cache__[moduleId] = {
1694016988
/******/ // no module.id needed

test/__snapshots__/ConfigTestCases.basictest.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15562,6 +15562,12 @@ console.log({ HomeLayout });
1556215562
/******/ if (cachedModule !== undefined) {
1556315563
/******/ return cachedModule.exports;
1556415564
/******/ }
15565+
/******/ // Check if module exists (development only)
15566+
/******/ if (__webpack_modules__[moduleId] === undefined) {
15567+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
15568+
/******/ e.code = 'MODULE_NOT_FOUND';
15569+
/******/ throw e;
15570+
/******/ }
1556515571
/******/ // Create a new module (and put it into the cache)
1556615572
/******/ var module = __webpack_module_cache__[moduleId] = {
1556715573
/******/ // no module.id needed
@@ -15726,6 +15732,12 @@ __WEBPACK_EXTERNAL_MODULE_externals1_v__;
1572615732
/******/ if (cachedModule !== undefined) {
1572715733
/******/ return cachedModule.exports;
1572815734
/******/ }
15735+
/******/ // Check if module exists (development only)
15736+
/******/ if (__webpack_modules__[moduleId] === undefined) {
15737+
/******/ var e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
15738+
/******/ e.code = 'MODULE_NOT_FOUND';
15739+
/******/ throw e;
15740+
/******/ }
1572915741
/******/ // Create a new module (and put it into the cache)
1573015742
/******/ var module = __webpack_module_cache__[moduleId] = {
1573115743
/******/ // no module.id needed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "helper";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
it("should throw helpful error when module is not found at runtime", function () {
2+
// First, require the helper module to get its id
3+
const helper = require("./helper");
4+
expect(helper).toBe("helper");
5+
6+
// Find the module id for helper.js
7+
const moduleId = Object.keys(__webpack_modules__).find(
8+
id => id.includes("helper")
9+
);
10+
expect(moduleId).toBeDefined();
11+
12+
// Delete the module from cache and modules to simulate a corrupted state
13+
delete __webpack_module_cache__[moduleId];
14+
delete __webpack_modules__[moduleId];
15+
16+
// Now trying to require the module should throw a helpful error matching Node.js format
17+
let thrownError;
18+
try {
19+
require("./helper");
20+
} catch (e) {
21+
thrownError = e;
22+
}
23+
24+
expect(thrownError).toBeDefined();
25+
expect(thrownError.message).toMatch(/Cannot find module/);
26+
expect(thrownError.code).toBe("MODULE_NOT_FOUND");
27+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
output: {
7+
pathinfo: true
8+
}
9+
};

0 commit comments

Comments
 (0)