npm
Features
bash
# Install dependencies
npm install
# Install package
npm install react
# Install dev dependency
npm install --save-dev typescript
# Install globally
npm install -g create-react-app
# Run scripts
npm run build
npm test
# Update packages
npm update
# Outdated packages
npm outdated
# Audit security
npm audit
npm audit fixPros
- Built into Node.js
- Largest package registry
- Excellent documentation
- Workspaces support
Cons
- Slow installation
- Duplicate node_modules
- Network overhead
yarn
Features
bash
# Install dependencies
yarn install
# Add package
yarn add react
# Add dev dependency
yarn add --dev typescript
# Run scripts
yarn build
yarn test
# Upgrade interactive
yarn upgrade-interactive --latest
# Workspaces
yarn workspace workspace-name add reactPros
- Faster than npm (parallel installs)
- PnP (Plug'n'Play) mode
- Better output formatting
- Workspaces built-in
Cons
- Not built into Node.js
- Additional installation
- Smaller community than npm
pnpm
Features
bash
# Install dependencies
pnpm install
# Add package
pnpm add react
# Add dev dependency
pnpm add -D typescript
# Run scripts
pnpm build
pnpm test
# Monorepo
pnpm --filter workspace-name add reactPros
- Fastest: Uses hard links/symlinks
- Disk efficient: Single copy on disk
- Strict: Prevents phantom dependencies
- Monorepo: Excellent workspaces support
Cons
- Different node_modules structure
- Compatibility issues with some tools
- Newer, smaller ecosystem
Performance Comparison
| Operation | npm | yarn | pnpm | |-----------|-----|------|------| | Install | Slow | Fast | Fastest | | Disk Usage | High | High | Low | | Boot Time | Slow | Fast | Fast | | Monorepos | Workspaces | Workspaces | Built-in |
Lock Files
json
// package-lock.json (npm)
// yarn.lock (yarn)
// pnpm-lock.yaml (pnpm)
// Commit lock file to version control!
// Ensures reproducible installsWorkspaces (Monorepos)
npm workspaces
json
// package.json
{
"workspaces": [
"packages/*"
]
}pnpm workspace
yaml
# pnpm-workspace.yaml
packages:
- 'packages/*'
- 'apps/*'Best Practices
- 1Lock files: Always commit to version control
- 2.npmrc: Configure registry, settings
- 3Engines: Specify Node version
- 4Scripts: Use npm scripts for tasks
- 5Audit: Regular security audits
Which to Choose?
Choose npm if:
- Starting new project
- Want simplicity
- Don't need workspaces
Choose yarn if:
- Need faster installs
- Want PnP mode
- Team uses yarn
Choose pnpm if:
- Monorepo setup
- Disk space matters
- Want strict dependencies
Migration
bash
# npm → yarn
yarn import
# npm/yarn → pnpm
pnpm import
# pnpm → npm
pnpm install && pnpm import && npm installChoose based on your needs!