Yarn Berry PnP strict mode "Cannot find module" in CI
Yarn Berry Plug-n-Play enforces declared dependencies strictly. A package you import but never listed in package.json is blocked, even though a hoisted node_modules layout would have tolerated it.
What this error means
A build or test fails under Yarn PnP with "X tried to access Y, but it is not declared in your dependencies". It works under npm/yarn classic because hoisting made the undeclared package reachable.
Error: Your application tried to access lodash, but it isn't declared in
your dependencies; this makes the require call ambiguous and unsound.
Required package: lodash
Required by: my-app@workspace:.Common causes
Undeclared (phantom) dependency
Code relied on a transitive package being hoisted into node_modules. PnP refuses to resolve packages you did not declare.
A dependency with its own undeclared imports
A third-party package imports something it never declared; PnP surfaces that latent bug.
How to fix it
Declare the dependency you import
Add the package directly to package.json so PnP can resolve it.
yarn add lodashPatch a dependency missing peer declarations
Use packageExtensions to add the missing dependency to a third-party package PnP rejects.
# .yarnrc.yml
packageExtensions:
"buggy-pkg@*":
dependencies:
lodash: "*"How to prevent it
- Declare every package you import directly.
- Use packageExtensions to fix third-party packages with missing deps.
- Run PnP installs in CI early to catch phantom dependencies.