Repository: webpack/webpack
File: lib/javascript/JavascriptModulesPlugin.js (JavaScript)

Code before:
					]);
		const needModuleId = runtimeRequirements.has(RuntimeGlobals.moduleId);
		const needModuleLoaded = runtimeRequirements.has(
			RuntimeGlobals.moduleLoaded
		);
		const needModuleDefer = runtimeRequirements.has(
			RuntimeGlobals.makeDeferredNamespaceObject
		);
		const content = Template.asString([
			"// Check if module is in cache",
			"var cachedModule = __webpack_module_cache__[moduleId];",
			"if (cachedModule !== undefined) {",
			outputOptions.strictModuleErrorHandling
				? Template.indent([
						"if (cachedModule.error !== undefined) throw cachedModule.error;",
						"return cachedModule.exports;"
					])
				: Template.indent("return cachedModule.exports;"),
			"}",
			// Add helpful error message in development mode when module is not found
			...(outputOptions.pathinfo
				? [
						"// 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.');"
						]),
						"}"
					]
				: []),
			"// Create a new module (and put it into the cache)",
			"var module = __webpack_module_cache__[moduleId] = {",
			Template.indent([
				needModuleId ? "id: moduleId," : "// no module.id needed",
				needModuleLoaded ? "loaded: false," : "// no module.loaded needed",
				needModuleDefer
					? "exports: __webpack_module_deferred_exports__[moduleId] || {}"
					: "exports: {}"
			]),
			"};",
			"",
			outputOptions.strictModuleExceptionHandling
				? Template.asString([
						"// Execute the module function",
						"var threw = true;",
						"try {",
						Template.indent([
							moduleExecution,
							"threw = false;",
							...(needModuleDefer
								? ["delete __webpack_module_deferred_exports__[moduleId];"]

Review comment (suggestion):
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](https://github.com/user-attachments/assets/98fa1e69-6824-4f1b-9576-9eff70652477)


Code after:
					]);
		const needModuleId = runtimeRequirements.has(RuntimeGlobals.moduleId);
		const needModuleLoaded = runtimeRequirements.has(
			RuntimeGlobals.moduleLoaded
		);
		const needModuleDefer = runtimeRequirements.has(
			RuntimeGlobals.makeDeferredNamespaceObject
		);
		const content = Template.asString([
			"// Check if module is in cache",
			"var cachedModule = __webpack_module_cache__[moduleId];",
			"if (cachedModule !== undefined) {",
			outputOptions.strictModuleErrorHandling
				? Template.indent([
						"if (cachedModule.error !== undefined) throw cachedModule.error;",
						"return cachedModule.exports;"
					])
				: Template.indent("return cachedModule.exports;"),
			"}",
			// Add helpful error message in development mode when module is not found
			...(outputOptions.pathinfo
				? [
						"// Check if module exists (development only)",
						"if (__webpack_modules__[moduleId] === undefined) {",
						Template.indent([
							'var e = new Error("Cannot find module \'" + moduleId + "\'");',
							"e.code = 'MODULE_NOT_FOUND';",
							"throw e;"
						]),
						"}"
					]
				: []),
			"// Create a new module (and put it into the cache)",
			"var module = __webpack_module_cache__[moduleId] = {",
			Template.indent([
				needModuleId ? "id: moduleId," : "// no module.id needed",
				needModuleLoaded ? "loaded: false," : "// no module.loaded needed",
				needModuleDefer
					? "exports: __webpack_module_deferred_exports__[moduleId] || {}"
					: "exports: {}"
			]),
			"};",
			"",
			outputOptions.strictModuleExceptionHandling
				? Template.asString([
						"// Execute the module function",
						"var threw = true;",
						"try {",
						Template.indent([
							moduleExecution,
							"threw = false;",