npm Cheat Sheet
Quick reference for npm commands, package.json fields, version ranges, scripts, and common patterns. Everything you need for Node.js package management.
Essential Commands
Scripts & Running
Version Ranges
package.json Key Fields
Publishing & Registry
Workspaces & Monorepo
Essential Commands
| npm init -y | Create package.json with defaults |
| npm install (npm i) | Install all dependencies from package.json |
| npm install pkg | Install and add to dependencies |
| npm install -D pkg | Install as devDependency |
| npm install -g pkg | Install globally |
| npm uninstall pkg | Remove package |
| npm update | Update all packages to latest allowed |
| npm outdated | Show packages needing updates |
| npm list --depth=0 | List top-level installed packages |
| npm audit / npm audit fix | Check and fix vulnerabilities |
Scripts & Running
| npm run dev | Run custom script "dev" |
| npm start | Run "start" script (no "run" needed) |
| npm test | Run "test" script (no "run" needed) |
| npm run build | Run "build" script |
| npx pkg | Run package without installing globally |
| npm run lint -- --fix | Pass args to script after -- |
| "prestart": "npm run build" | Pre-hook: runs before "start" |
| "postinstall": "husky" | Post-hook: runs after "install" |
| "scripts": {"dev": "next dev"} | Define custom scripts in package.json |
Version Ranges
| "^1.2.3" | Compatible: >=1.2.3 <2.0.0 (default) |
| "~1.2.3" | Patch only: >=1.2.3 <1.3.0 |
| "1.2.3" | Exact version only |
| ">=1.0.0 <2.0.0" | Range with comparison |
| "*" / "latest" | Any version / latest tag |
| "1.x" / "1.2.x" | Wildcard for minor/patch |
| npm version patch | Bump 1.2.3 → 1.2.4 |
| npm version minor | Bump 1.2.3 → 1.3.0 |
| npm version major | Bump 1.2.3 → 2.0.0 |
package.json Key Fields
| "name": "my-pkg" | Package name (lowercase, no spaces) |
| "version": "1.0.0" | Semver version |
| "main": "index.js" | Entry point for require() |
| "module": "index.mjs" | ES module entry point |
| "type": "module" | Use ES modules (.js as ESM) |
| "engines": {"node": ">=18"} | Required Node.js version |
| "files": ["dist"] | Files included when published |
| "bin": {"cli": "./cli.js"} | CLI executable mapping |
| "exports": {".":" ./dist/index.js"} | Package exports map (Node 12+) |
| "private": true | Prevent accidental npm publish |
Publishing & Registry
| npm login | Authenticate with npm registry |
| npm publish | Publish package to npm |
| npm publish --tag beta | Publish with dist-tag |
| npm deprecate pkg "msg" | Mark version as deprecated |
| npm pack | Create tarball without publishing |
| npm link | Symlink local package for development |
| npm config set registry URL | Use custom registry |
| .npmrc | Per-project npm configuration file |
Workspaces & Monorepo
| "workspaces": ["packages/*"] | Define workspace directories |
| npm install -w pkg-a | Install in specific workspace |
| npm run build -w pkg-a | Run script in specific workspace |
| npm run build --workspaces | Run script in all workspaces |
| npm ls --workspaces | List dependencies across workspaces |