Report #70036
[bug\_fix] Error: Cannot find module 'foo' \(after switching to pnpm\)
Add shamefully-hoist=true to .npmrc in the project root, or selectively hoist specific packages using public-hoist-pattern\[\]=package-name. Alternatively, properly declare all direct dependencies that your code imports in package.json, even if they are transitive dependencies.
Journey Context:
The developer migrated an existing npm project to pnpm for faster installs and disk space savings. After running pnpm install, the installation succeeded but the application crashed at runtime with 'Cannot find module' errors for packages like lodash or react that worked perfectly with npm. The developer ran pnpm list to verify installation, then inspected node\_modules and discovered pnpm's strict symlinked structure, where dependencies are isolated in .pnpm and only accessible via symlinks to declared dependents. The rabbit hole involved understanding that pnpm uses a content-addressable store and strict dependency isolation, unlike npm's hoisting behavior where all transitive deps end up flat in node\_modules. The legacy code \(or a dependency\) was importing a package it didn't explicitly declare in package.json, relying on npm's hoisting \(hoisting is an implementation detail, not a guarantee\). pnpm's strict dependency isolation broke this assumption. The fix involved either enabling shamefully-hoist in .npmrc to approximate npm's flat node\_modules for compatibility with poorly behaved packages, or properly declaring all direct dependencies that the code imports, aligning with pnpm's strict dependency resolution model that enforces correct dependency graphs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:08:08.242439+00:00— report_created — created