TL;DR
Was: Schneller, speicherplatzeffizienter Paketmanager für Node.js.
Warum: 2x schneller als npm, spart Speicherplatz, strikte Abhängigkeiten, hervorragende Monorepo-Unterstützung.
Quick Start
Installieren:
# npm
npm install -g pnpm
# Homebrew
brew install pnpm
# Check version
pnpm --version
Grundlegende Verwendung:
pnpm init
pnpm add express
pnpm install
pnpm dev
Cheatsheet
| Befehl | Beschreibung |
|---|---|
pnpm add pkg | Abhängigkeit hinzufügen |
pnpm add -D pkg | Dev-Abhängigkeit hinzufügen |
pnpm remove pkg | Paket entfernen |
pnpm install | Alle Abhängigkeiten installieren |
pnpm update | Pakete aktualisieren |
pnpm run script | Skript ausführen |
pnpm dlx pkg | Paket ausführen (wie npx) |
Gotchas
Pakete installieren
# Production dependency
pnpm add express
# Dev dependency
pnpm add -D typescript
# Global package
pnpm add -g nodemon
# Specific version
pnpm add [email protected]
# From workspace
pnpm add @myorg/shared --filter @myorg/web
Workspaces (Monorepo)
# pnpm-workspace.yaml
packages:
- 'packages/*'
- 'apps/*'
# Install all workspace deps
pnpm install
# Run script in specific package
pnpm --filter @myorg/web dev
# Run in all packages
pnpm -r run build
# Add dep to specific package
pnpm add lodash --filter @myorg/utils
# Add workspace dependency
pnpm add @myorg/shared --filter @myorg/web --workspace
Filterung
# By package name
pnpm --filter @myorg/web dev
# By directory
pnpm --filter ./packages/web dev
# All packages
pnpm -r run test
# Packages with changes
pnpm --filter "...[origin/main]" run test
# Dependencies of package
pnpm --filter "@myorg/web..." run build
Konfiguration (.npmrc)
# Strict mode (recommended)
strict-peer-dependencies=true
auto-install-peers=true
# Shamefully hoist (compatibility mode)
shamefully-hoist=true
# Store location
store-dir=~/.pnpm-store
# Registry
registry=https://registry.npmmirror.com
Migration von npm/yarn
# Import from package-lock.json
pnpm import
# Or just delete and reinstall
rm -rf node_modules package-lock.json yarn.lock
pnpm install
Skripte
{
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest"
}
}
# Run script
pnpm dev
pnpm run build
# Run binary
pnpm exec vitest
# Execute package without installing
pnpm dlx create-react-app my-app
Next Steps
- pnpm Documentation - Offizielle Dokumentation
- Workspaces - Monorepo-Anleitung
- Filtering - Paketauswahl
- pnpm vs npm vs yarn - Benchmarks