📚 3 min read
pnpm Guide ​
pnpm is a fast, disk space efficient package manager that creates a non-flat node_modules directory. It uses hard links and symlinks to save disk space and boost installation speed.
Installation ​
bash
# Using npm
npm install -g pnpm
# Using Curl (Unix)
curl -fsSL https://get.pnpm.io/install.sh | sh -
# Using PowerShell (Windows)
iwr https://get.pnpm.io/install.ps1 -useb | iex
# Check installation
pnpm --versionKey Commands ​
Project Initialization ​
bash
# Create a new package.json
pnpm init
# Create with defaults
pnpm init -yPackage Installation ​
bash
# Install all dependencies
pnpm install
# Add a package
pnpm add package-name
# Add as dev dependency
pnpm add -D package-name
# Add globally
pnpm add -g package-name
# Add specific version
pnpm add package-name@versionPackage Management ​
bash
# Update packages
pnpm update
# Remove package
pnpm remove package-name
# List installed packages
pnpm list
# Check outdated packages
pnpm outdated
# Run security audit
pnpm auditScripts ​
bash
# Run a script
pnpm run script-name
# or simply
pnpm script-name
# Common commands
pnpm start
pnpm test
pnpm buildConfiguration ​
pnpm Configuration File (.npmrc) ​
ini
# Set registry
registry=https://registry.npmjs.org/
# Use strict-peer-dependencies
strict-peer-dependencies=true
# Enable workspace features
node-linker=hoisted
# Set store directory
store-dir=.pnpm-storepackage.json ​
json
{
"name": "my-project",
"version": "1.0.0",
"packageManager": "pnpm@8.0.0",
"scripts": {
"start": "node index.js",
"test": "jest",
"build": "webpack"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.0"
}
}Workspaces ​
pnpm has excellent monorepo support through workspaces:
yaml
# pnpm-workspace.yaml
packages:
- 'packages/*'
- 'apps/*'
- '!**/test/**'Workspace Commands ​
bash
# Install dependencies for all workspaces
pnpm install
# Run command in specific workspace
pnpm --filter package-name run command
# Run command in all workspaces
pnpm -r run command
# Run command in parallel
pnpm -r --parallel run commandStore & Linking ​
pnpm uses a unique approach to manage dependencies:
Content-addressable Store ​
bash
# Location of global store
~/.pnpm-store/
# Project-specific store
node_modules/.pnpm/Hard Links ​
- Each version of a package is saved only once on disk
- Multiple projects share the same package versions
- Significant disk space savings
Best Practices ​
Version Control
- Commit
pnpm-lock.yaml - Use
.gitignorefornode_modules - Consider committing
.npmrc - Specify
packageManagerin package.json
- Commit
Security
- Use
pnpm auditregularly - Enable strict-peer-dependencies
- Use official registry
- Keep pnpm updated
- Use
Performance
- Use workspace features for monorepos
- Enable hoisting when needed
- Leverage parallel execution
- Use store-dir for CI caching
Dependency Management
- Regular dependency updates
- Use overrides for resolution conflicts
- Check for peer dependency issues
- Use
pnpm whyfor dependency analysis
Common Issues and Solutions ​
Store Issues ​
bash
# Clear store
pnpm store prune
# Verify store
pnpm store verify
# Remove unused packages
pnpm store pruneDependency Resolution ​
bash
# Force resolution
pnpm add package-name --force
# Clean install
pnpm install --forceMigration from npm/Yarn ​
bash
# Import package-lock.json/yarn.lock
pnpm import
# Generate pnpm-lock.yaml
pnpm installAdvanced Features ​
Filtering ​
bash
# Run in packages that depend on another
pnpm --filter ...package-name command
# Run in packages updated since main
pnpm --filter "[main]" command
# Run in specific packages
pnpm --filter "package-a...package-b" commandHooks ​
json
{
"pnpm": {
"hooks": {
"readPackage": "hooks/readPackage.js"
}
}
}Custom Configs ​
js
// .pnpmfile.cjs
module.exports = {
hooks: {
readPackage(pkg) {
// Modify package here
return pkg;
},
},
};Publishing ​
bash
# Publish package
pnpm publish
# Publish with custom tag
pnpm publish --tag beta
# Publish workspace packages
pnpm -r publishMore content coming soon...