8000 esm: fix loading of CJS modules from ESM · nodejs/node@8acbe6d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 8acbe6d

Browse files
aduh95RafaelGSS
authored andcommitted
esm: fix loading of CJS modules from ESM
PR-URL: #49500 Fixes: #49497 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
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

    Lines changed: 2 additions & 1 deletion
    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;
    Lines changed: 7 additions & 0 deletions
    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

    Lines changed: 6 additions & 0 deletions

    test/fixtures/recursive-b.cjs

    Lines changed: 3 additions & 0 deletions
    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');
    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)
    0