Skip to content
Latchkey

Yarn Berry PnP "Cannot find module" (require stack) in CI

Yarn Berry's default Plug'n'Play mode has no node_modules; resolution goes through .pnp.cjs. A tool that resolves modules the classic way, unaware of PnP, cannot find its dependencies and throws "Cannot find module".

What this error means

A build or lint step fails with "Error: Cannot find module 'X'" and a require stack, even though yarn install succeeded, because the tool bypasses the PnP resolver.

node
Error: Cannot find module 'eslint-plugin-react'
Require stack:
- /home/runner/work/app/app/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader)

Common causes

A tool does not support Plug'n'Play

The tool resolves modules by walking node_modules, which does not exist under PnP, so it never consults .pnp.cjs.

The process was not launched through the PnP runtime

Running a binary directly instead of via yarn run skips the .pnp.cjs hook, so PnP resolution is not active.

How to fix it

Run tools through the PnP runtime

Invoke tools with yarn <tool> (or the sdk shims) so .pnp.cjs is loaded and resolution works.

Terminal
yarn eslint .
yarn dlx @yarnpkg/sdks base

Switch to the node-modules linker for stubborn tools

If a critical tool cannot support PnP, set nodeLinker: node-modules so Yarn produces a conventional layout.

.yarnrc.yml
nodeLinker: node-modules

How to prevent it

  • Install editor/CLI SDKs with yarn dlx @yarnpkg/sdks so tools resolve under PnP.
  • Launch tools via yarn so .pnp.cjs is active.
  • Choose nodeLinker: node-modules up front if your toolchain is PnP-incompatible.

Frequently asked questions

What causes ""Cannot find module" under Plug'n'Play"?
The tool resolves modules by walking node_modules, which does not exist under PnP, so it never consults .pnp.cjs.
How do I fix "Cannot find module" under Plug'n'Play?
Invoke tools with yarn <tool> (or the sdk shims) so .pnp.cjs is loaded and resolution works.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card