2 min read

Why PNPM is Better than Yarn and NPM

Why PNPM is Better than Yarn and NPM
Midjourney

pnpm stands for performant npm, and you can see what they took issue with when they built it.

NPM, and node_modules in general, have a history of being slow, bloated, and horribly abused.

To address this, pnpm tackles the problem in 3 ways.

Saves Disk Space

When you use npm, a dependency will be saved every time you use it in a project. Even if the dependencies are cached, they will still be copied to each project when installed. With pnpm, the dependency is stored in a content-addressable store and linked when installed.

This allows for projects to share the same dependencies, which results in significant disk space savings and faster installation.

Faster installation

To get a faster installation, pnpm executes a slightly different algorithm than npm.

  1. Dependency resolution: It first figures out all the dependencies the project needs.
  2. Directory structure calculation: Then, it decides how the dependencies will be organized in the node_modules folder.
  3. Linking dependencies: Finally, it links the dependencies from a central store to your project's node_modules folder, instead of copying them, saving time and disk space.

Creating a non-flat node_modules directory

pnpm can link modules within the node_modules directory. This stops packages from needing to reinstall their dependencies in their node_modules directory. Instead, they all link to the central pnpm repo. This causes the entire node_modules directory to be much flatter and easier to work with.

An illustration of a node_modules directory created by pnpm. Packages in the root node_modules are symlinks to directories inside the node_modules/.pnpm directory

Features

Lastly, here is a list of features from the pnpm site that compares what it can do vs the other package managers. For the most part, any package manager can do the important tasks.

FeaturepnpmYarnnpm
Workspace support✔️✔️✔️
Isolated node_modules✔️ - The default✔️✔️
Hoisted node_modules✔️✔️✔️ - The default
Autoinstalling peers✔️✔️
Plug'n'Play✔️✔️ - The default
Zero-Installs✔️
Patching dependencies✔️✔️
Managing Node.js versions✔️
Has a lockfile✔️ - pnpm-lock.yaml✔️ - yarn.lock✔️ - package-lock.json
Overrides support✔️✔️ - Via resolutions✔️
Content-addressable storage✔️
Dynamic package execution✔️ - Via pnpm dlx✔️ - Via yarn dlx✔️ - Via npx
Side-effects cache✔️
Listing licenses✔️ - Via pnpm licenses list✔️ - Via a plugin

Those are the big reasons why pnpm is becoming more popular. When working with lots of projects, or big projects, the small gains on installation speed and space compound. And as you know, engineers are always looking to speed things up a little.