NPM Vs Yarn Vs PNPM
npm vs. yarn vs. pnpm: Package Manager Vergleich
Die Wahl des Package Managers beeinflusst Performance und Workflow. Vergleichen Sie npm, yarn und pnpm für Ihr Projekt.
Übersicht
| Feature | npm | yarn | pnpm |
|---|---|---|---|
| Lockfile | package-lock.json | yarn.lock | pnpm-lock.yaml |
| Workspaces | ✅ | ✅ | ✅ |
| Disk-Effizienz | Schlecht | Schlecht | Excellent |
| Install Speed | Mittel | Schnell | Am schnellsten |
Installation
# npm: Mit Node.js dabei node -v && npm -v # yarn npm install -g yarn # pnpm npm install -g pnpm # oder curl -fsSL https://get.pnpm.io/install.sh | sh -
Basis-Befehle
# Dependencies installieren npm install yarn pnpm install # Package hinzufügen npm install lodash yarn add lodash pnpm add lodash # Dev Dependency npm install -D jest yarn add -D jest pnpm add -D jest # Global npm install -g pkg yarn global add pnpm add -g pkg # Package entfernen npm uninstall lodash yarn remove lodash pnpm remove lodash # Scripts ausführen npm run build yarn build pnpm build npm test yarn test pnpm test # Cache leeren npm cache clean yarn cache clean pnpm store prune
pnpm: Warum so schnell?
# npm/yarn: Jedes Projekt kopiert alle Packages project-a/node_modules/lodash/ # 2 MB project-b/node_modules/lodash/ # 2 MB (Kopie!) project-c/node_modules/lodash/ # 2 MB (Kopie!) → 6 MB total # pnpm: Content-addressable Store + Symlinks ~/.pnpm-store/lodash-4.17.21/ # 2 MB (einmal!) project-a/node_modules/.pnpm/lodash → ~/.pnpm-store/... project-b/node_modules/.pnpm/lodash → ~/.pnpm-store/... project-c/node_modules/.pnpm/lodash → ~/.pnpm-store/... → 2 MB total + Symlinks
Workspaces (Monorepo)
# package.json (npm & yarn)
{
"name": "monorepo",
"private": true,
"workspaces": [
"packages/*"
]
}
# pnpm-workspace.yaml
packages:
- 'packages/*'
- 'apps/*'
# Struktur
monorepo/
├── package.json
├── pnpm-workspace.yaml
├── packages/
│ ├── utils/
│ │ └── package.json
│ └── ui/
│ └── package.json
└── apps/
└── web/
└── package.json
# Befehle
pnpm install # Alle installieren
pnpm --filter utils test # Nur utils testen
pnpm --filter './packages/*' build # Alle packages bauen
pnpm add lodash --filter utils # Zu utils hinzufügen
Lockfiles
# IMMER committen! # .gitignore sollte NICHT enthalten: # package-lock.json # yarn.lock # pnpm-lock.yaml # CI: Exakt wie lokal installieren npm ci # Schneller als npm install yarn --frozen-lockfile pnpm install --frozen-lockfile
Security
# npm npm audit npm audit fix npm audit fix --force # yarn yarn audit yarn audit --fix # Nur yarn v1 # pnpm pnpm audit pnpm audit --fix
Performance-Vergleich
# Benchmark (ungefähre Werte) Clean Install (node_modules leer): npm: 45s yarn: 25s pnpm: 15s Mit Cache: npm: 15s yarn: 8s pnpm: 3s # pnpm gewinnt durch: # - Hardlinks statt Kopien # - Symlinked node_modules # - Parallele Downloads
Migration
# npm → pnpm rm -rf node_modules package-lock.json pnpm import # Konvertiert package-lock.json pnpm install # yarn → pnpm rm -rf node_modules yarn.lock pnpm import # Konvertiert yarn.lock pnpm install # Empfehlung für neue Projekte: pnpm # Für bestehende Projekte: Bei aktuellem bleiben
Wann welchen?
- npm: Standard, überall verfügbar, wenig Setup
- yarn: Bewährt, gute Workspaces, Plug'n'Play Option
- pnpm: Beste Performance, Disk-sparend, strikt