Skip to content

Commit

Permalink
esm: fix loading of CJS modules from ESM
Browse files Browse the repository at this point in the history
PR-URL: #49500
Fixes: #49497
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
  • Loading branch information
aduh95 authored Sep 6, 2023
1 parent cfe4166 commit fdc65d9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
// obtained by calling the monkey-patchable CJS loader.
const cjsLoader = source == null ? (module, source, url, filename) => {
try {
module.load(filename);
assert(module === CJSModule._cache[filename]);
CJSModule._load(filename);
} catch (err) {
enrichCJSError(err, source, url);
throw err;
Expand Down
7 changes: 7 additions & 0 deletions test/es-module/test-esm-recursive-cjs-dependencies.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '../common/index.mjs';
import { strictEqual } from 'node:assert';

import '../fixtures/recursive-a.cjs';

strictEqual(global.counter, 1);
delete global.counter;
6 changes: 6 additions & 0 deletions test/fixtures/recursive-a.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

global.counter ??= 0;
global.counter++;

require('./recursive-b.cjs');
3 changes: 3 additions & 0 deletions test/fixtures/recursive-b.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./recursive-a.cjs');

0 comments on commit fdc65d9

Please sign in to comment.