question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
merceyzcommented, Nov 2, 2021

This is fixed by https://github.com/yarnpkg/berry/pull/3667, you can verify by running

yarn set version from sources --branch 3667
0reactions
flying-sheepcommented, Jan 31, 2022

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

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... If the file specified by path is not found, an error is returned....
Read more >
Dynamic require in webpack not working
We need to resolve those dynamic requires using webpack functionality [https://webpack.github.io/docs/context.html][1]. Just need to mention the directory ...
Read more >
Troubleshooting
This error message means that Plug'n'Play was able to locate the package part of the require (so for example it understood that foo...
Read more >
require() of es module not supported
Error : require() of ES modules is not supported when importing node-fetch ... Instead rename index.js to end in .cjs, change the requiring...
Read more >
Understanding (all) JavaScript module formats and tools
CJS module: CommonJS module, or Node.js module ... Use dynamic AMD module. define(require => { const dynamicDependencyModule1 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found