Commit f0890131725 for nodejs
commit f0890131725c7cfc491865d25fde2c2a88f43e9a
Author: Prashant Singh Sengar <168135528+Pranshu12354@users.noreply.github.com>
Date: Sun Nov 29 18:56:08 2026 +0530
module: fix require() of re-exported CJS dep after cache deletion
The fast path in Module._load() returned getExportsForCircularRequire()
for any unloaded cache entry found via relativeResolveCache, without
the kIsCachedByESMLoader/kModuleCircularVisited checks the regular
cache path performs. Cache entries pre-created by the ESM loader for
re-exported dependencies (cjsPreparseModuleExports()) were therefore
mistaken for circular requires on re-import, returning empty exports.
Mirror the regular-path checks in the fast path and fall through to
loading for an unvisited ESM-loader cache entry.
Fixes: https://github.com/nodejs/node/issues/67611
Signed-off-by: Prashant Singh Sengar <prashantsengar2003@gmail.com>
Assisted-by: Muse
PR-URL: https://github.com/nodejs/node/pull/67761
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 94d13c47bd5..0ce49516f9c 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -1366,12 +1366,22 @@ Module._load = function(request, parent, isMain, internalOptions = kEmptyObject)
const cachedModule = Module._cache[filename];
if (cachedModule !== undefined) {
updateChildren(parent, cachedModule, true);
- if (!cachedModule.loaded) {
+ if (cachedModule.loaded) {
+ return cachedModule.exports;
+ }
+ // Mirror the checks in the regular cache path below: only treat
+ // an unloaded entry as a circular require if it was not cached by
+ // the ESM loader, or if it has already been visited while loading.
+ // An unvisited entry cached by the ESM loader (e.g. pre-created
+ // for a re-export by cjsPreparseModuleExports()) has not been
+ // loaded yet, so fall through and load it.
+ if (!cachedModule[kIsCachedByESMLoader] ||
+ cachedModule[kModuleCircularVisited]) {
return getExportsForCircularRequire(cachedModule);
}
- return cachedModule.exports;
+ } else {
+ delete relResolveCacheByDir[request];
}
- delete relResolveCacheByDir[request];
}
}
diff --git a/test/parallel/test-module-cjs-reexport-reimport.mjs b/test/parallel/test-module-cjs-reexport-reimport.mjs
new file mode 100644
index 00000000000..4e4bb3ef837
--- /dev/null
+++ b/test/parallel/test-module-cjs-reexport-reimport.mjs
@@ -0,0 +1,42 @@
+// Regression test for https://github.com/nodejs/node/issues/67611
+// Re-importing a CommonJS module whose exports are a re-export
+// (module.exports = require('./dep.cjs')) after both modules were deleted
+// from require.cache must re-evaluate the dependency instead of returning
+// the empty exports of the unloaded cache entry pre-created by the ESM
+// loader for the re-export.
+
+import '../common/index.mjs';
+import assert from 'node:assert';
+import { realpathSync, writeFileSync } from 'node:fs';
+import { createRequire } from 'node:module';
+import { join } from 'node:path';
+import { pathToFileURL } from 'node:url';
+import tmpdir from '../common/tmpdir.js';
+
+tmpdir.refresh();
+
+const require = createRequire(import.meta.url);
+const dir = realpathSync(tmpdir.path);
+const entry = join(dir, 'entry.cjs');
+const dep = join(dir, 'dep.cjs');
+
+writeFileSync(entry, "module.exports = require('./dep.cjs')\n");
+
+for (const value of [1, 2, 3]) {
+ writeFileSync(dep, `module.exports = { value: ${value} }\n`);
+ delete require.cache[entry];
+ delete require.cache[dep];
+ const { default: entryExports } =
+ await import(`${pathToFileURL(entry)}?v=${value}`);
+ assert.deepStrictEqual(entryExports, { value });
+}
+
+// A syntax error in the re-exported dependency must surface on re-import,
+// not be swallowed into empty exports.
+writeFileSync(dep, 'module.exports = {\n');
+delete require.cache[entry];
+delete require.cache[dep];
+await assert.rejects(
+ import(`${pathToFileURL(entry)}?v=4`),
+ SyntaxError,
+);