Skip to content

Commit 8acbe6d

Browse files
aduh95RafaelGSS
authored andcommitted
esm: fix loading of CJS modules from ESM
PR-URL: #49500 Fixes: #49497 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 6b5d5b2 commit 8acbe6d

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

lib/internal/modules/esm/translators.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
279279
// obtained by calling the monkey-patchable CJS loader.
280280
const cjsLoader = source == null ? (module, source, url, filename) => {
281281
try {
282-
module.load(filename);
282+
assert(module === CJSModule._cache[filename]);
283+
CJSModule._load(filename);
283284
} catch (err) {
284285
enrichCJSError(err, source, url);
285286
throw err;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../common/index.mjs';
2+
import { strictEqual } from 'node:assert';
3+
4+
import '../fixtures/recursive-a.cjs';
5+
6+
strictEqual(global.counter, 1);
7+
delete global.counter;

test/fixtures/recursive-a.cjs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
global.counter ??= 0;
4+
global.counter++;
5+
6+
require('./recursive-b.cjs');

test/fixtures/recursive-b.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
require('./recursive-a.cjs');

0 commit comments

Comments
 (0)