Initialization
| npm | yarn | Description |
|---|---|---|
npm init | yarn init | Interactive initialization |
npm init -y | yarn init -y | Initialize with defaults |
Package Installation
| npm | yarn | Description |
|---|---|---|
npm install | yarn | Install dependencies |
npm install pkg | yarn add pkg | Add package |
npm install -D pkg | yarn add -D pkg | Add as dev dependency |
npm install -g pkg | yarn global add pkg | Install globally |
npm install pkg@1.0.0 | yarn add pkg@1.0.0 | Specific version |
npm install pkg@latest | yarn add pkg@latest | Install latest |
Package Removal
| npm | yarn | Description |
|---|---|---|
npm uninstall pkg | yarn remove pkg | Remove package |
npm uninstall -g pkg | yarn global remove pkg | Remove globally |
Package Update
| npm | yarn | Description |
|---|---|---|
npm update | yarn upgrade | Update all packages |
npm update pkg | yarn upgrade pkg | Update specific package |
npm outdated | yarn outdated | Show outdated packages |
Information
| npm | yarn | Description |
|---|---|---|
npm list | yarn list | List installed packages |
npm list --depth=0 | yarn list --depth=0 | Top-level only |
npm info pkg | yarn info pkg | Package information |
npm search keyword | - | Search packages |
npm view pkg versions | yarn info pkg versions | Available versions |
Script Execution
| npm | yarn | Description |
|---|---|---|
npm run script | yarn script | Run script |
npm start | yarn start | Run start script |
npm test | yarn test | Run test script |
npm run build | yarn build | Run build script |
npx pkg | yarn dlx pkg | Execute temporarily |
Cache
| npm | yarn | Description |
|---|---|---|
npm cache clean --force | yarn cache clean | Clear cache |
npm cache verify | - | Verify cache |
Lock Files
| npm | yarn | Description |
|---|---|---|
package-lock.json | yarn.lock | Lock file name |
npm ci | yarn install --frozen-lockfile | Strict install from lock |
Publishing
| npm | yarn | Description |
|---|---|---|
npm login | yarn login | Login to registry |
npm publish | yarn publish | Publish package |
npm unpublish pkg | - | Unpublish package |
npm version patch | yarn version --patch | Bump patch version |
npm version minor | yarn version --minor | Bump minor version |
npm version major | yarn version --major | Bump major version |
Configuration
| npm | yarn | Description |
|---|---|---|
npm config list | yarn config list | List config |
npm config set key value | yarn config set key value | Set config |
npm config get key | yarn config get key | Get config |
Audit & Security
| npm | yarn | Description |
|---|---|---|
npm audit | yarn audit | Check vulnerabilities |
npm audit fix | - | Auto-fix vulnerabilities |
Workspaces
| npm | yarn | Description |
|---|---|---|
npm init -w packages/a | - | Initialize workspace |
npm install -w a pkg | yarn workspace a add pkg | Add to workspace |
npm run -w a script | yarn workspace a script | Run in workspace |
npm run --workspaces script | yarn workspaces run script | Run in all workspaces |
Common Command Combinations
| Command | Description |
|---|---|
rm -rf node_modules && npm install | Clean install |
npm list --depth=0 | grep pkg | Search package |
npm ls pkg | Show package dependents |
npm explain pkg | Show detailed dependencies |
npm dedupe | Deduplicate packages |
npm prune | Remove unused packages |