Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: fix loading of (recursive) CJS modules from ESM #49500

Merged
merged 1 commit into from
Sep 6, 2023
Merged
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
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);
JakobJingleheimer marked this conversation as resolved.
Show resolved Hide resolved
} 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++;
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

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');
Loading