Skip to content

Resolving module.createRequire throws error when mixedModules: false #543

@timfish

Description

@timfish

Support for createRequire was added in:

However, this only works with mixedModules: true.

If this mode is not enabled, you get:

    TypeError: Cannot read properties of undefined (reading 'shadowDepth')

      623 |                                 node.arguments[0].type === 'Literal' &&
      624 |                                 node.callee.type === 'Identifier' &&
    > 625 |                                 knownBindings.require.shadowDepth === 0) {
          |                                                       ^
      626 |                                 await processRequireArg(node.arguments[0]);
      627 |                             }
      628 |                             break;

      at Object.shadowDepth (out/analyze.js:625:55)
      at AsyncWalker.visit (node_modules/estree-walker/dist/umd/estree-walker.js:234:6)
      at AsyncWalker.visit (node_modules/estree-walker/dist/umd/estree-walker.js:271:7)
      at AsyncWalker.visit (node_modules/estree-walker/dist/umd/estree-walker.js:264:15)
      at AsyncWalker.visit (node_modules/estree-walker/dist/umd/estree-walker.js:264:15)
      at asyncWalk (node_modules/estree-walker/dist/umd/estree-walker.js:336:10)
      at analyze (out/analyze.js:508:5)
      at Job.emitDependency (out/node-file-trace.js:340:29)
          at async Promise.all (index 0)
      at nodeFileTrace (out/node-file-trace.js:62:5)
      at doTrace (test/unit.test.js:153:37)
      at Object.<anonymous> (test/unit.test.js:342:5)

The error at this line is because knownBindings.require is undefined when the file is ESM and mixedModules: false:

knownBindings.require.shadowDepth === 0

This was not picked up buy the tests because it looks like they all have mixedModules enabled:

mixedModules: true,

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions