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: nodejs#49500
Fixes: nodejs#49497
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
  • Loading branch information
aduh95 authored and alexfernandez committed Nov 1, 2023
1 parent 8ea49be commit d5305bf
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 d5305bf

Please sign in to comment.