pnpm - Fast Package Manager
pnpm (Performant npm) is a Node.js package manager. It serves the same purpose as npm and yarn, but it stores packages in a global store using hard links, which makes it strong in both installation speed and disk usage.
Differences from npm
Section titled “Differences from npm”npm copies packages into each project’s node_modules folder. If 10 projects use react, you get 10 copies.
pnpm uses hard links, which are references to files, and stores each package only once in a global store. Even if multiple projects use the same package, the physical data on disk exists only once.
In diagram form:
[With npm]
project-a/node_modules/react/ ← copy of react (15MB)
project-b/node_modules/react/ ← copy of react (15MB)
project-c/node_modules/react/ ← copy of react (15MB)
Total: 45MB used
[With pnpm]
~/.pnpm-store/react/ ← actual react data (15MB) only
project-a/node_modules/react ← link (almost 0 bytes)
project-b/node_modules/react ← link (almost 0 bytes)
project-c/node_modules/react ← link (almost 0 bytes)
Total: 15MB used| Feature | npm | pnpm |
|---|---|---|
| Installation | Built into Node.js | Requires separate installation |
| Installation speed | Standard | Fast, with strong cache efficiency |
| Disk usage | Copies per project | Large savings with a global store |
| Dependency resolution | Loose (can access undeclared packages) | Strict (only declared packages are accessible) |
| Command compatibility | - | Nearly the same as npm |
Installation
Section titled “Installation”Via npm (Recommended)
Section titled “Via npm (Recommended)”If Node.js and npm are already installed, you can install pnpm with:
npm install -g pnpm
pnpm --version # Check versionUsing Corepack (Node.js v16.9 or later)
Section titled “Using Corepack (Node.js v16.9 or later)”Corepack is a package-manager management tool that comes with Node.js. It lets you manage pnpm versions per project.
corepack enable # Enable Corepack
corepack prepare pnpm@latest --activate # Activate the latest pnpm
pnpm --version # Check versionNotes for nvm Users
Section titled “Notes for nvm Users”If you use nvm, switching Node.js versions can make a globally installed pnpm unavailable. That is because nvm keeps global packages separate per Node.js version.
Fix 1: Use Corepack
Corepack is not affected by nvm version switching. You can keep using pnpm after changing Node.js versions.
Fix 2: Reinstall after switching versions
nvm use 22
npm install -g pnpm # Reinstall after switchingMain Commands Compared with npm
Section titled “Main Commands Compared with npm”The command structure is close to npm, so it feels familiar if you have used npm before.
| Action | npm | pnpm |
|---|---|---|
| Install dependencies | npm install | pnpm install |
| Add a package | npm install <pkg> | pnpm add <pkg> |
| Add a dev dependency | npm install -D <pkg> | pnpm add -D <pkg> |
| Run a script | npm run dev | pnpm run dev or pnpm dev |
| Remove a package | npm uninstall <pkg> | pnpm remove <pkg> |
| Global install | npm install -g <pkg> | pnpm add -g <pkg> |
| Show list | npm list | pnpm list |
| Check for updates | npm outdated | pnpm outdated |
Which Should You Use, npm or pnpm?
Section titled “Which Should You Use, npm or pnpm?”When to Choose npm
Section titled “When to Choose npm”- When you are just starting with JavaScript - It comes with Node.js, so you can use it right away without extra setup
- For simple personal projects - If you only use a few packages, npm is enough
- When your team is used to npm - In existing projects or teams where everyone already uses npm, stability may be worth more than migration cost
When to Choose pnpm
Section titled “When to Choose pnpm”- When you manage multiple projects in parallel - The global store can reduce both disk usage and installation time after the first run
- When you want faster CI/CD installs - Caching works well and shortens test and build time
- When disk space is limited - If the same package is used in multiple projects, pnpm stores only one physical copy
- When you use a monorepo - pnpm is strong at workspace management
When to Migrate
Section titled “When to Migrate”Start projects with npm, then consider pnpm when you hit situations such as:
node_modulesbecoming too large and using too much disk spacenpm installtaking too long- Repeatedly installing the same libraries across many projects
Migration is easy. In an existing project with a package.json, running pnpm install is enough.
Notes When Using pnpm
Section titled “Notes When Using pnpm”pnpm is stricter than npm about dependency management. Packages not explicitly listed in package.json cannot be accessed from your code. This helps prevent bugs, but some libraries may need configuration changes.
In that case, create a .npmrc file in the project root and add:
# .npmrc (only when needed)
shamefully-hoist=trueBecause this loosens pnpm’s strict dependency handling, keep it to the minimum necessary.
Summary
Section titled “Summary”- pnpm is a faster package manager with better disk efficiency than npm
- Install it with
npm install -g pnpm - In nvm environments, Corepack helps avoid problems when switching versions
- The command structure is very similar to npm, so migration cost is low
- It is especially useful when managing multiple projects or when installation speed matters
Frequently Asked Questions
Section titled “Frequently Asked Questions”Q: Do I need pnpm if I already have npm?
A: It is not required, but you may get better installation speed and lower disk usage. The benefits are especially noticeable when you work on multiple projects or have limited disk space.
Q: Will existing npm projects still work with pnpm?
A: Yes. In an existing project with a package.json, running pnpm install is enough. However, it will create pnpm-lock.yaml instead of package-lock.json.
Q: What is the difference between yarn and pnpm?
A: Yarn is also an alternative to npm, but pnpm’s strengths are disk efficiency through its global store and strict dependency management. They are close in features, so choose based on project policy and existing setup.
Q: Should pnpm-lock.yaml be committed to Git?
A: Yes. Like package-lock.json, pnpm-lock.yaml ensures that everyone on the team uses the same package versions. It should be included in Git.
Next Step
Section titled “Next Step”- npm - The Standard Node.js Package Manager
- Node.js Installation and Initial Setup
- nvm - Node.js Version Manager