Skip to content

[Bug?]: PnP fails to resolve dynamic require from CJS #3687

@kherock

Description

@kherock

Self-service

  • I'd be willing to implement a fix

Describe the bug

Attempting to use import() from a CJS file fails under PnP despite Node being loaded with --experimental-loader=.../.pnp.loader.mjs. This appears to be a gap in the recent support added for the experimental ESM loader as the issue is not present when the script is loaded as ESM.

To reproduce

https://github.com/kherock/pnp-exports-map-import-bug

You can reproduce the error by running the following after checking out:

$ yarn node test.js
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'my-package' imported from /Users/herockk/Workspaces/pnp-exports-map-import/test.js

The failure disappears when loading the script as .mjs:

$ yarn node test.mjs
(node:58073) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
[Module: null prototype] { default: { foo: 'bar' }, foo: 'bar' }

The error is not present with nodeLinker: node-modules set in .yarnrc.yml.

Environment

System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.9.1 - /private/var/folders/9f/kl_c86q5651fqmhmzf63jb0x5t_d9j/T/xfs-7d04f2cf/node
    Yarn: 3.1.0 - /private/var/folders/9f/kl_c86q5651fqmhmzf63jb0x5t_d9j/T/xfs-7d04f2cf/yarn
    npm: 7.21.1 - ~/.volta/tools/image/node/16.9.1/bin/npm

Additional context

I'm running Next.js 12 which generates server-side code with imports in the form of dynamic import() calls. The package I'm trying to import is @react-hook/passive-layout-effect which has a package.json in a format similar to the reproduction linked above. NextJS doesn't support running under a project with { "type": "module" } yet, so the workaround of loading the script as ESM isn't an option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingesm

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions