pnpm
Using pnpm as the package manager
pnpm is a fast, disk space efficient package manager that the template uses for dependency management. pnpm stands for "performant npm" and provides several advantages over npm and yarn.
Overview
pnpm provides:
- Speed: Faster installation and dependency resolution
- Efficiency: Saves disk space by using a shared store
- Strictness: Prevents dependency conflicts with strict peer dependencies
- Modern: Designed for modern JavaScript and TypeScript projects
- Compatible: Works with all npm packages and registries
Why pnpm?
The template uses pnpm because:
- Performance: Faster than npm and yarn in most cases
- Disk Space: Uses hard links to save disk space
- Security: More secure dependency resolution
- Strictness: Better handling of peer dependencies
- Workspaces: Excellent support for monorepos and workspaces
- Modern: Actively maintained with regular updates
Package.json Configuration
The template specifies pnpm as the package manager:
{
"packageManager": "pnpm@11.6.0"
}This ensures that all users of the template use the same package manager version.
Using pnpm
Installation
Install pnpm globally:
# Using npm
npm install -g pnpm
# Using curl
curl -fsSL https://get.pnpm.io/install.sh | sh
# Using Homebrew (macOS)
brew install pnpmVerify installation:
pnpm --versionBasic Commands
| Command | Description | npm Equivalent |
|---|---|---|
pnpm install | Install dependencies | npm install |
pnpm add <package> | Add a dependency | npm install <package> |
pnpm add -D <package> | Add a dev dependency | npm install --save-dev <package> |
pnpm remove <package> | Remove a dependency | npm uninstall <package> |
pnpm update | Update dependencies | npm update |
pnpm list | List installed packages | npm list |
pnpm run <script> | Run a script | npm run <script> |
pnpm test | Run the test script | npm test |
Install Dependencies
Install all dependencies from package.json:
pnpm installThis will:
- Install all dependencies listed in package.json
- Create or update pnpm-lock.yaml
- Set up the node_modules directory with symlinks to the pnpm store
Add Dependencies
Add a new dependency:
# Add a regular dependency
pnpm add lodash
# Add a development dependency
pnpm add -D typescript
# Add a specific version
pnpm add lodash@4.17.21
# Add a peer dependency
pnpm add -P react
# Add an optional dependency
pnpm add -O optional-packageRemove Dependencies
Remove a dependency:
pnpm remove lodashUpdate Dependencies
Update dependencies:
# Update all dependencies
pnpm update
# Update specific packages
pnpm update lodash typescript
# Update to latest versions
pnpm up
# Update to latest major versions
pnpm up --latestpnpm Features
Workspaces
pnpm has excellent support for workspaces (monorepos):
{
"pnpm-workspace.yaml": "packages/*"
}The template includes a basic workspace configuration:
# pnpm-workspace.yaml
packages:
- "."Shared Store
pnpm uses a shared store to save disk space:
# View the pnpm store location
pnpm store path
# List all packages in the store
pnpm store listStrict Peer Dependencies
pnpm automatically installs peer dependencies:
# Install with strict peer dependencies (default)
pnpm install
# Install without peer dependencies
pnpm install --shamefully-hoistFiltering
Filter dependencies by package name:
# List only production dependencies
pnpm list --prod
# List only development dependencies
pnpm list --dev
# List dependencies matching a pattern
pnpm list lodashPerformance Tips
Faster Installs
- Use
--frozen-lockfilefor CI/CD to skip lockfile updates - Use
--prefer-offlineto prefer offline mode - Use
--strict-peer-dependenciesto skip peer dependency checks
# Fast install for CI/CD
pnpm install --frozen-lockfile --prefer-offlineDisk Space Optimization
- pnpm automatically uses hard links to save space
- The shared store prevents duplicate installations
- Use
pnpm store pruneto clean up unused packages
# Clean up unused packages from the store
pnpm store pruneAdvanced Usage
Scripts
Run scripts with pnpm:
# Run a script
pnpm run build
# Run a script with arguments
pnpm run test -- --watch
# Run a script in a specific directory
pnpm -C packages/my-package run buildEnvironment Variables
Set environment variables for scripts:
# Set environment variable
NODE_ENV=production pnpm run build
# Set multiple environment variables
NODE_ENV=production DEBUG=true pnpm run buildConfiguration
Configure pnpm with .npmrc or pnpm-config.json:
# .npmrc
shamefully-hoist=true
strict-peer-dependencies=false
store-dir=/path/to/storeGlobal Packages
Install and manage global packages:
# Install a global package
pnpm add -g create-react-app
# List global packages
pnpm list -g
# Remove a global package
pnpm remove -g create-react-appMigration from npm/yarn
From npm
-
Remove node_modules and package-lock.json:
rm -rf node_modules package-lock.json -
Install pnpm:
npm install -g pnpm -
Install dependencies with pnpm:
pnpm install
From yarn
-
Remove node_modules and yarn.lock:
rm -rf node_modules yarn.lock -
Install pnpm:
npm install -g pnpm -
Install dependencies with pnpm:
pnpm install
Troubleshooting
Common Issues
-
Installation fails:
- Check your Node.js version (pnpm requires Node.js 14.6+)
- Try clearing the store:
pnpm store prune - Try with
--force:pnpm install --force
-
Peer dependency conflicts:
- Use
--shamefully-hoistto ignore peer dependencies - Or add the peer dependency explicitly
- Use
-
Slow installation:
- Use
--prefer-offlineto use cached packages - Check your network connection
- Try with a different registry
- Use
-
Permission issues:
- Check file permissions in the project directory
- Try running with sudo (not recommended)
- Fix ownership with
chown
Debug Mode
Run pnpm with debug logging:
DEBUG=pnpm* pnpm installBest Practices
Use pnpm in CI/CD
Configure your CI/CD to use pnpm:
# GitHub Actions example
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfileUse pnpm in Docker
FROM node:18
# Install pnpm
RUN npm install -g pnpm
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Install dependencies
RUN pnpm install --frozen-lockfile
# Copy source files
COPY . .
# Build
RUN pnpm run buildUse pnpm Workspaces for Monorepos
For monorepo projects:
# pnpm-workspace.yaml
packages:
- "packages/*"
- "apps/*"Commit pnpm-lock.yaml
Always commit the pnpm-lock.yaml file to ensure consistent installations across all environments.