npm Cheatsheet
Quick reference for npm commands for project setup, dependency management, scripts, updates, and package inspection
npm is the default package manager for Node.js. This cheatsheet covers common npm commands for starting projects, installing dependencies, running scripts, updating packages, inspecting dependency trees, and maintaining package metadata.
Project Setup
Create and inspect project metadata.
| Command | Description |
|---|---|
npm init | Create package.json interactively |
npm init -y | Create package.json with defaults |
npm pkg get name | Print package name |
npm pkg set scripts.start="node index.js" | Add or update a script |
npm pkg delete scripts.old | Remove a package.json field |
Install Dependencies
Add project, development, and global packages.
| Command | Description |
|---|---|
npm install | Install project dependencies |
npm install package | Install and save dependency |
npm install -D package | Install development dependency |
npm install -g package | Install global CLI package |
npm install package@1.2.3 | Install a specific version |
npm install package@latest | Install latest published version |
npm install --no-save package | Install without saving |
Clean Installs
Reproduce dependency trees from lock files.
| Command | Description |
|---|---|
npm ci | Clean install from package-lock.json |
npm ci --omit=dev | Install production dependencies only |
npm install --omit=dev | Skip development dependencies |
npm install --package-lock-only | Update lock file only |
npm install --ignore-scripts | Install without lifecycle scripts |
Versions and Updates
Check outdated packages and update dependency ranges.
| Command | Description |
|---|---|
npm outdated | Show packages with newer versions |
npm update | Update within package.json ranges |
npm update package | Update one package |
npm view package version | Show latest package version |
npm view package versions | List published versions |
npm version patch | Bump package patch version |
npm version minor | Bump package minor version |
Remove and Clean
Uninstall packages and clear generated files.
| Command | Description |
|---|---|
npm uninstall package | Remove dependency |
npm uninstall -D package | Remove development dependency |
npm uninstall -g package | Remove global package |
npm prune | Remove extraneous packages |
npm dedupe | Reduce duplicate dependencies |
npm cache verify | Verify npm cache |
npm cache clean --force | Clear npm cache |
Scripts and Execution
Run package scripts and one-off tools.
| Command | Description |
|---|---|
npm run | List available scripts |
npm run script | Run a named script |
npm start | Run the start script |
npm test | Run the test script |
npm run build -- --watch | Pass args to script |
npx package | Run package binary once |
npm create vite@latest app | Run create package |
Inspect Packages
List dependencies and package metadata.
| Command | Description |
|---|---|
npm list | List direct dependencies |
npm list --all | List full dependency tree |
npm list -g --depth=0 | List global packages |
npm explain package | Explain why package is installed |
npm root | Show local node_modules path |
npm root -g | Show global node_modules path |
Configuration
View and change npm settings.
| Command | Description |
|---|---|
npm config list | Show current config |
npm config get prefix | Show global install prefix |
npm config set prefix ~/.npm-global | Set global install prefix |
npm config get registry | Show registry URL |
npm config set registry url | Set registry URL |
npm whoami | Show logged-in npm user |
npm login | Log in to npm registry |
Security and Audit
Check dependency risks and funding metadata.
| Command | Description |
|---|---|
npm audit | Scan dependencies for advisories |
npm audit fix | Apply compatible security fixes |
npm audit --omit=dev | Audit production dependencies |
npm fund | Show funding links |
npm doctor | Check npm environment |
npm ping | Test registry connection |
Related Guides
Use these references for Node.js and package workflows.
| Guide | Description |
|---|---|
npm Command | Full npm tutorial with examples |
Install Node.js on Ubuntu 26.04 | Install Node.js and npm |
Install Yarn on Ubuntu 20.04 | Alternative package manager |