Report #45095
[bug\_fix] Error: Your application tried to access \[package\], but it isn't declared in your dependencies \(Yarn PnP\)
Add the missing package to the workspace package's dependencies in package.json \(do not rely on transitive dependencies\), then run yarn install. Alternatively, run the Node.js script through Yarn \(yarn node script.js\) instead of node script.js to ensure the PnP loader is active, or configure the IDE using yarn dlx @yarnpkg/sdks vscode to support PnP. Root cause: Yarn 2\+ \(Berry\) uses Plug'n'Play \(PnP\) by default, which strictly enforces that packages can only import dependencies explicitly declared in their own package.json, preventing "phantom dependencies" where a package relies on transitive dependencies being hoisted.
Journey Context:
Developer migrates from Yarn 1 to Yarn 2 \(Berry\) or clones a repository using Yarn 2. They run yarn install and notice that node\_modules is either missing or contains only .yarn/cache and .pnp.cjs. They try to run their application with node src/index.js and immediately get "Error: Your application tried to access lodash, but it isn't declared in your dependencies" even though lodash is installed \(transitively via another package\). They try adding lodash to dependencies, which works, but they wonder why they have to explicitly declare it when it worked before. They learn that Yarn 2 uses Plug'n'Play which forbids accessing undeclared dependencies to ensure strict dependency contracts. They also discover they should use yarn node instead of node to run scripts, and that they need to setup SDKs for their IDE \(VSCode\) so that IntelliSense works with the virtual .pnp.cjs file instead of node\_modules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:09:34.914705+00:00— report_created — created