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

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

Closed
1 task
kherock opened this issue Nov 1, 2021 · 5 comments · Fixed by #3667
Closed
1 task

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

kherock opened this issue Nov 1, 2021 · 5 comments · Fixed by #3667
Labels
bug Something isn't working esm

Comments

@kherock
Copy link
Contributor

kherock commented Nov 1, 2021

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.

@kherock kherock added the bug Something isn't working label Nov 1, 2021
@merceyz merceyz added the esm label Nov 2, 2021
@merceyz
Copy link
Member

merceyz commented Nov 2, 2021

This is fixed by #3667, you can verify by running

yarn set version from sources --branch 3667

@NMinhNguyen
Copy link

Having done yarn set version canary (which installs 3.2.0-rc.8), I'm still unable to run yarn node test.js from the repro 😕

@merceyz
Copy link
Member

merceyz commented Dec 22, 2021

Did you run yarn install first?

@NMinhNguyen
Copy link

Did you run yarn install first?

Ah nope, that fixed it, thanks! So I'm guessing the newer Yarn modifies the PnP map to make it work correctly?

@flying-sheep
Copy link
Contributor

Ahahaha, that’s why I ended up filing #4045!

Any reason yarn set version canary doesn’t do yarn install after?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working esm
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants