Report #64136
[bug\_fix] pnpm: Error: Cannot find module 'X' ... code: 'MODULE\_NOT\_FOUND'
Configure .npmrc with shamefully-hoist=true to hoist all dependencies to root, or use public-hoist-pattern\[\]=\*eslint\* to selectively hoist specific packages, or add the missing package as a direct dependency.
Journey Context:
Migrating a Create React App or Next.js project from npm/yarn to pnpm to save disk space. After running pnpm install, the install succeeds, but running pnpm dev or pnpm build fails immediately with MODULE\_NOT\_FOUND for a package \(e.g., 'eslint-scope' or '@types/react'\) that is clearly present in the node\_modules subdirectory of a dependency. With npm, this worked due to hoisting \(flat node\_modules\). Inspecting pnpm's node\_modules reveals a strict structure where dependencies are isolated and only accessible via exact symlinks. The package throwing the error is trying to require a package it doesn't explicitly declare, relying on the hoisting behavior \(phantom dependency\). Researching pnpm docs, find that pnpm uses a content-addressable store and non-flat node\_modules by design for strictness. The shamefully-hoist=true option in .npmrc flattens the node\_modules structure to mimic npm/yarn, allowing phantom dependencies to resolve. Alternatively, public-hoist-pattern can selectively hoist only specific problematic packages \(like eslint plugins or types\) without full flattening. Adding the package as a direct dependency also fixes it by ensuring it's in the project's direct node\_modules. The fix works by restoring the module resolution algorithm expected by legacy tooling.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T14:08:36.215693+00:00— report_created — created