Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
pnpm 10.x reference skill covering workspaces, catalogs, patches, peer deps, overrides, and CI/CD caching strategies.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/best-practices-performance.md
1---2name: pnpm-performance-optimization3description: Tips and tricks for faster installs and better performance4---56# pnpm Performance Optimization78pnpm is fast by default, but these optimizations can make it even faster.910## Install Optimizations1112### Use Frozen Lockfile1314Skip resolution when lockfile exists:1516```bash17pnpm install --frozen-lockfile18```1920This is faster because pnpm skips the resolution phase entirely.2122### Prefer Offline Mode2324Use cached packages when available:2526```bash27pnpm install --prefer-offline28```2930### Skip Optional Dependencies3132If you don't need optional deps:3334```bash35pnpm install --no-optional36```3738### Skip Scripts3940For CI or when scripts aren't needed:4142```bash43pnpm install --ignore-scripts44```4546**Caution:** Some packages require postinstall scripts to work correctly.4748### Only Build Specific Dependencies4950Build-script approval is a single `allowBuilds` map (replaces `onlyBuiltDependencies`/`neverBuiltDependencies`). Only allowed packages run install scripts:5152```yaml title="pnpm-workspace.yaml"53allowBuilds:54esbuild: true55'@swc/core': true56core-js: false # explicitly skip57```5859Packages not listed are treated as unreviewed (blocked by default). See `features-supply-chain-security` for the full build-approval workflow.6061## Store Optimizations6263### Side Effects Cache6465Cache native module build results (enabled by default):6667```yaml title="pnpm-workspace.yaml"68sideEffectsCache: true69```7071This caches the results of postinstall scripts, speeding up subsequent installs.7273### Global Virtual Store7475For many checkouts of the same repo (e.g. git worktrees / multiple agents), enable the global virtual store so each project's `node_modules` is just symlinks into one shared store — near-zero per-checkout cost. Auto-disabled in CI.7677```yaml title="pnpm-workspace.yaml"78enableGlobalVirtualStore: true79```8081### Shared Store8283A single content-addressable store is used for all projects by default:8485```yaml title="pnpm-workspace.yaml"86storeDir: ~/.local/share/pnpm/store87```8889Benefits: packages downloaded once, hard links save disk space, faster cached installs.9091### Store Maintenance9293Periodically clean unused packages:9495```bash96# Remove unreferenced packages97pnpm store prune9899# Check store integrity100pnpm store status101```102103## Workspace Optimizations104105### Parallel Execution106107Run workspace scripts in parallel:108109```bash110pnpm -r --parallel run build111```112113Control concurrency:114```yaml title="pnpm-workspace.yaml"115workspaceConcurrency: 8116```117118### Stream Output119120See output in real-time:121122```bash123pnpm -r --stream run build124```125126### Filter to Changed Packages127128Only build what changed:129130```bash131# Build packages changed since main branch132pnpm --filter "...[origin/main]" run build133```134135### Topological Order136137Build dependencies before dependents:138139```bash140pnpm -r run build141# Automatically runs in topological order142```143144For explicit sequential builds:145```bash146pnpm -r --workspace-concurrency=1 run build147```148149## Network Optimizations150151Network/registry settings are camelCase in `pnpm-workspace.yaml` (registry URLs may also go in `registries`):152153```yaml title="pnpm-workspace.yaml"154registries:155default: https://registry.npmmirror.com/156fetchRetries: 3157fetchRetryMintimeout: 10000158fetchRetryMaxtimeout: 60000159networkConcurrency: 16 # auto: clamp(workers x 3, 16, 64)160httpProxy: http://proxy.company.com:8080161httpsProxy: http://proxy.company.com:8080162```163164## Lockfile Optimization165166### Single Lockfile (Monorepos)167168Use shared lockfile for all packages (default):169170```yaml title="pnpm-workspace.yaml"171sharedWorkspaceLockfile: true172```173174Benefits:175- Single source of truth176- Faster resolution177- Consistent versions across workspace178179### Lockfile-only Mode180181Only update lockfile without installing:182183```bash184pnpm install --lockfile-only185```186187## Benchmarking188189### Compare Install Times190191```bash192# Clean install193rm -rf node_modules pnpm-lock.yaml194time pnpm install195196# Cached install (with lockfile)197rm -rf node_modules198time pnpm install --frozen-lockfile199200# With store cache201time pnpm install --frozen-lockfile --prefer-offline202```203204### Profile Resolution205206Debug slow installs:207208```bash209# Verbose logging210pnpm install --reporter=append-only211212# Debug mode213DEBUG=pnpm:* pnpm install214```215216## Configuration Summary217218Optimized `pnpm-workspace.yaml` for performance:219220```yaml title="pnpm-workspace.yaml"221# Install behavior222autoInstallPeers: true223sideEffectsCache: true224optimisticRepeatInstall: true225226# Build approval (only what's necessary)227allowBuilds:228esbuild: true229'@swc/core': true230231# Network232fetchRetries: 3233networkConcurrency: 16234235# Workspace236workspaceConcurrency: 4237238# Many checkouts of the same repo239enableGlobalVirtualStore: true240```241242## Quick Reference243244| Scenario | Command/Setting |245|----------|-----------------|246| CI installs | `pnpm ci` / `pnpm install --frozen-lockfile` |247| Offline development | `--prefer-offline` |248| Control native builds | `allowBuilds` map |249| Parallel workspace | `pnpm -r --parallel run build` |250| Build changed only | `pnpm --filter "...[origin/main]" build` |251| Clean store | `pnpm store prune` |252| Many worktrees/agents | `enableGlobalVirtualStore: true` |253254<!--255Source references:256- https://pnpm.io/settings257- https://pnpm.io/cli/install258- https://pnpm.io/filtering259- https://pnpm.io/global-virtual-store260-->261262