Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,12 +1376,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];
}
}

Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-module-cjs-reexport-reimport.mjs
Original file line number Diff line number Diff line change
@@ -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,
);