Sphere
Log in Join

pnpm vs npm vs Yarn: Why I Switched and What Broke

I moved two projects from npm to pnpm last year. One went smoothly. The other broke in a way that took a morning to understand. This is the comparison I wish I had read before starting.

What pnpm actually does differently

npm and Yarn install packages into a flat node_modules tree at each project. pnpm keeps a single content-addressable store on disk and links packages into your project. If five projects use the same version of React, that version is stored once.

On my machine, moving a monorepo with twelve packages took node_modules from about 2.1 GB to 380 MB. That is the main reason I switched, and it is still the best reason.

It also changes what your code can see. With npm, a dependency of a dependency is often hoisted into the top-level node_modules, so your code can import a package you never declared. pnpm does not do that. This is correct behavior, and it is what broke the second project.

The breakage: phantom dependencies

Our build script imported lodash.merge directly. It was not in package.json; it came along as a dependency of something else. It worked with npm because of hoisting. With pnpm:

Error: Cannot find module 'lodash.merge'

The fix is not to change pnpm's linker settings. The fix is to declare the package:

pnpm add lodash.merge

We found three other imports like that. A quick way to catch them is to switch to pnpm, run your build, and see what fails. Every missing module is a dependency you were never declaring.

If you absolutely cannot change the code, pnpm has a node-linker=hoisted setting in .npmrc. I would treat it as a temporary bridge, not a destination.

Lockfiles matter more than the installer

Do not keep package-lock.json and pnpm-lock.yaml in the same repository. Pick one. When we migrated, I deleted the npm lockfile in the same commit as the switch, so there was no ambiguity about which one CI should trust.

CI also needs the same package manager. If your workflow still runs npm ci while developers run pnpm install, you will get different dependency trees and a very confusing bug report. Change the workflow in the same commit.

Commands I actually use

pnpm install          # install from lockfile
pnpm add <pkg> # add a dependency
pnpm add -D <pkg> # add a dev dependency
pnpm remove <pkg>
pnpm run dev
pnpm dlx create-vite # like npx, without installing globally
pnpm exec eslint . # run a local binary

The pnpm dlx and pnpm exec distinction confused me at first. exec runs something installed in the project; dlx downloads something temporarily and runs it. Using npx out of habit is the most common mistake when people first switch.

Monorepos are where it earns its keep

For a single small project, the difference between npm and pnpm is modest. For a monorepo, pnpm workspaces are simple:

packages:
- 'packages/*'

in pnpm-workspace.yaml, then pnpm -r install installs everything. Running a script across packages is pnpm -r test. We dropped a custom script that had been keeping package links in sync.

When I would stay on npm

If your team is small, your dependencies are simple, and nobody wants to learn a second tool, npm is fine. The migration cost is real: lockfile changes, CI changes, and a morning of phantom dependency fixes. It was worth it for us because disk space and the monorepo were genuine problems.

Tool comparisons usually argue about speed. In practice the thing that decided it for us was strict dependency resolution. It caught a sloppy habit we did not know we had.