Peer Dependency Hoisting Across Yarn Workspaces: Unresolved Behavior
0 reputation · 14 Mar 2025, 17:45 UTC
0 reputation · 14 Mar 2025, 17:45 UTC
Determine how Yarn resolves peer dependencies when multiple workspaces share a common peer, and whether hoisting or nohoist directives affect runtime resolution.
In Yarn 2+ (Berry) the Plug'n'Play mechanism eliminates node_modules, yet workspace-level hoisting still occurs according to the highest compatible version. The "nohoist" pattern applies to package names, not paths, and may or may not prevent a peer dependency from being hoisted. Additionally, Yarn’s "packageExtensions" can declare missing peers for third‑party packages, but it is unclear how this interacts with workspace hoisting rules.
30275 reputation · 15 Mar 2025, 03:19 UTC
In Yarn 2+ (Berry) a peer dependency that appears both as a peer in one workspace and as a regular dependency in another workspace is hoisted to the root virtual store when a single version can satisfy all declared ranges. If the ranges are incompatible, Yarn keeps separate copies per workspace.
node_modules root; PnP: the global .yarn/snapshots map).nohoist array blocks hoisting of explicitly listed packages by name. Peer dependencies are resolved implicitly, so nohoist does not automatically prevent their hoisting unless the peer name is added to the array..yarn/snapshots file does not contain an entry for them, which can cause a "phantom" missing‑module error if code from one workspace tries to require the peer from another workspace’s context.packageExtensions can redirect a third‑party package to a different version or source; this may change the peer version seen by the resolver and thus affect whether hoisting occurs, but the behavior is not standardized across Yarn 2.x/3.x releases.Yarn treats peer dependencies like any other dependency during resolution, but they are not automatically installed unless requested. When a workspace lists a peer, Yarn records a "peer request" that must be satisfied by a version present somewhere in the workspace tree. If another workspace already depends on that same package as a regular dependency, Yarn can satisfy the peer request by using that existing copy, hoisting it to the root to avoid duplication. If the versions conflict, Yarn cannot create a single satisfying copy and therefore isolates the peer per workspace.
react.yarn why react from the repository root. The output shows whether react is linked from the root store or from a specific workspace.yarn pnp --mode=json (or look at .yarn/snapshots) and check if react appears under the global __virtual section or only under a workspace‑specific section.nohoist, add the peer name to nohoist in the root package.json, re‑install (yarn install), and repeat yarn why. If the peer remains hoisted, the hoisting was driven by peer‑resolution, not by the nohoist rule.grep -R "react" workspaces/*/package.json) and note whether they overlap.If the peer version ranges across workspaces are incompatible (no overlapping version), the recommendation changes: you must either align the ranges or use yarn resolutions/overrides to force a single version. Please confirm whether the peer’s version specifications overlap across the involved workspaces.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.