Report #102426
[bug\_fix] pnpm / Cannot find module 'lodash' after switching from npm or yarn Classic
Add the missing package as a direct dependency of the code that imports it \(\`pnpm add lodash\`\), or hoist known phantom dependencies via \`pnpm-workspace.yaml\`: \`publicHoistPattern: \['\*lodash\*'\]\` / \`shamefullyHoist: true\` only as a last resort. Do not rely on transitive dependencies being reachable.
Journey Context:
A project works fine under npm but fails under pnpm with \`Cannot find module 'lodash'\` even though \`lodash\` exists somewhere in \`node\_modules\`. The reason is that npm/Yarn Classic hoist transitive dependencies to the top level, so any file can accidentally \`require\('lodash'\)\` even if it is not declared in that package's \`package.json\`. pnpm creates a strict, non-flat \`node\_modules\` where each package can only see what it declared. When you switch, code that was relying on hoisting breaks. The correct fix is to declare the dependency wherever it is imported. If the phantom dependency is inside a third-party tool that cannot be patched, use \`publicHoistPattern\` to hoist only that package to the root, or \`shamefullyHoist: true\` to reproduce npm's flat layout. \`shamefullyHoist\` defeats pnpm's safety guarantees, so prefer targeted hoisting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:51:06.338541+00:00— report_created — created