By Rohit Yadav on May 29, 2025
7 min read
Package management is a crucial aspect of modern JavaScript development. While npm has long been the default package manager for Node.js projects, pnpm has gained significant popularity due to its efficiency, disk space savings, and improved dependency management. In this guide, I'll walk you through migrating your projects from npm to pnpm with minimal hassle.
pnpm (performant npm) is a fast, disk space efficient package manager that uses a unique approach to managing dependencies. Unlike npm or Yarn, which create copies of packages in each project's node_modules
directory, pnpm uses a content-addressable store and creates hard links or symlinks to the packages. This approach has several advantages:
Before migrating to pnpm, ensure you have:
First, you need to install pnpm globally:
npm install -g pnpm
Alternatively, you can use the recommended installation methods:
For Windows (PowerShell):
iwr https://get.pnpm.io/install.ps1 -useb | iex
For macOS/Linux:
curl -fsSL https://get.pnpm.io/install.sh | sh -
Start by removing npm's lock file and the node_modules directory:
rm package-lock.json
rm -rf node_modules
If your project uses npm scripts in package.json, you can keep using them. pnpm supports the same commands with the same syntax, just replace npm
with pnpm
:
npm install
→ pnpm install
npm run dev
→ pnpm dev
npm run build
→ pnpm build
npm test
→ pnpm test
Reinstall your dependencies using pnpm:
pnpm install
This command will:
Create a .npmrc
file in your project root to configure pnpm:
shamefully-hoist=true
strict-peer-dependencies=false
The shamefully-hoist
option helps with compatibility for applications that expect a flat node_modules structure. You can remove this once your project is fully compatible with pnpm's default directory structure.
If you're using CI/CD pipelines, update your configuration files:
GitHub Actions example:
- name: Install dependencies
run: |
npm install -g pnpm
pnpm install
CircleCI example:
- run:
name: Install dependencies
command: |
npm install -g pnpm
pnpm install
Update your project documentation to reflect the switch to pnpm:
## Development
This project uses pnpm for dependency management.
### Install dependencies
```bash
pnpm install
pnpm dev
If you encounter errors about missing dependencies, try using the --shamefully-hoist
flag:
pnpm install --shamefully-hoist
Some packages may not be compatible with pnpm's node_modules structure. In such cases:
.pnpmfile.cjs
file:function readPackage(pkg) {
if (pkg.name === 'problematic-package') {
// Fix the package configuration
}
return pkg;
}
module.exports = {
hooks: {
readPackage
}
};
public-hoist-pattern[]=*problematic-package*
After migrating, you'll enjoy several benefits:
Migrating from npm to pnpm is a straightforward process that can significantly improve your development workflow. While there might be a few compatibility issues to solve initially, the long-term benefits in terms of performance, disk space usage, and dependency management make it well worth the effort.
Have you migrated your projects to pnpm? Share your experience and any tips you discovered along the way!
Drop me a message and let's bring your idea to life.
Drop your email below and I will get back to you soon.
Made with Love by Rohit Yadav
Portfolio inspired by Manu Arora