esm: fix loading of CJS modules from ESM

PR-URL: https://github.com/nodejs/node/pull/49500
Fixes: https://github.com/nodejs/node/issues/49497
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
This commit is contained in:
Antoine du Hamel 2023-09-06 12:12:14 +02:00 committed by GitHub
parent cfe4166f0f
commit fdc65d9769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 1 deletions

View File

@ -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;

View File

@ -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
test/fixtures/recursive-a.cjs vendored Normal file
View File

@ -0,0 +1,6 @@
'use strict';
global.counter ??= 0;
global.counter++;
require('./recursive-b.cjs');

3
test/fixtures/recursive-b.cjs vendored Normal file
View File

@ -0,0 +1,3 @@
'use strict';
require('./recursive-a.cjs');