Report #78485
[bug\_fix] Yarn PnP Error: Your application tried to access X, but it isn't declared in your dependencies
Root cause: Yarn 2\+ with Plug'n'Play \(PnP\) mode does not use node\_modules. Instead, it generates a .pnp.cjs file that maps imports strictly. Unlike node\_modules hoisting, a package can only require what it explicitly declares in its package.json dependencies. If package A tries to require package B but B is only a transitive dependency \(dependency of a dependency\), PnP blocks it because A has no explicit dependency on B. The fix is to use the packageExtensions feature in .yarnrc.yml to declare the missing dependency for the offending package \(e.g., telling Yarn that 'package-a' has a peer dependency on 'lodash'\), or to switch to nodeLinker: node-modules in .yarnrc.yml to use classic node\_modules \(sacrificing PnP benefits\).
Journey Context:
You migrate a project to Yarn Berry \(Yarn 2/3/4\) and run yarn install. It succeeds quickly, but when you run the app with node index.js, you get a YARN\_PNP\_ERROR stating that package 'express' tried to access 'body-parser' but it's not in its dependencies. You inspect node\_modules but find it's missing—Yarn PnP doesn't create it. You check express's package.json and see body-parser is indeed not listed \(it's actually a dependency of express historically, but in this version it's internal\). The error occurs because your code or another package is requiring body-parser directly, assuming hoisting. You search Yarn docs and find packageExtensions. You add to .yarnrc.yml: packageExtensions: 'express@\*': dependencies: 'body-parser': '\*'. You run yarn install again to update the PnP map. Now the resolution works because Yarn explicitly allows express \(or the requesting package\) to access body-parser as if it were declared. The app starts without the access error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:20:00.980099+00:00— report_created — created