pnpm wins in monorepos, npm wins on zero friction
Run du -sh node_modules on two projects: a small one with fifteen dependencies, and a monorepo with four apps sharing internal packages. On the first one, you won't notice any difference between npm and pnpm. On the second one, if you're still on npm, you're going to have the same copy of React, TypeScript and the same devDependencies repeated four times on disk — once for every workspace package…
In comparison to small projects with fewer dependencies, pnpm shows significant advantages when used in monorepos containing multiple packages that share dependencies. The reason behind this is that pnpm installs each dependency as a single, global copy instead of duplicating the same package across all packages. This design choice results in reduced disk space usage and faster build times across all projects within the monorepo.
The key principle of pnpm's approach is its content-addressable store, which stores packages in an external, shared location. Each package's node_modules folder then contains a symlink to this shared store rather than multiple copies of the same dependency. This leads to disk space savings that scale with the number of packages in the workspace, as all projects can share the same version of a library.
However, the benefits of using pnpm are not universal and depend on the specific characteristics of the project. For instance, if a project has only a single package.json file with no workspaces and a limited set of production dependencies, the difference in install time between npm and pnpm may be negligible. In such cases, the added complexity of adopting pnpm could outweigh the potential benefits.
A crucial consideration when evaluating pnpm is the impact on existing tooling and workflows. Projects that rely on npm-specific scripts or Docker images that assume a particular npm behavior may encounter issues when switching to pnpm. Migrating to pnpm requires not only changes to the lockfile format but also adjustments to Dockerfiles or other CI/CD configurations that depend on the lockfile structure.
In summary, the decision to adopt pnpm should be based on a careful assessment of the project's structure, dependency sharing, and existing tooling. Monorepos with multiple packages sharing dependencies, CI pipelines with dependency caching, and teams already familiar with pnpm's model are likely to see tangible benefits. Conversely, small projects with minimal dependencies and well-documented npm scripts may find no significant advantages to migrating to pnpm.
Ultimately, the choice between npm and pnpm should be driven by the specific needs and constraints of each project, rather than a one-size-fits-all recommendation.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.