npm Commands: Cheat Sheet with Examples
npm commands cheat sheet: install, update and remove packages, npm ci, run scripts and npx, audit and fix vulnerabilities, versions and publishing.
npm commands grouped by job: starting a project, installing, updating and removing packages, running scripts, checking for vulnerabilities, looking up package details and publishing your own. Parts in angle brackets are what you replace. The most useful difference to know is between npm install, which can update package-lock.json, and npm ci, which installs exactly what the lockfile says and fails if it doesn't match package.json, which is why build servers use it. npx runs a package's command without adding it to your project, handy for project generators and one-off tools. npm audit fix --force can jump to new major versions, so test afterwards. The Git Commands cheat sheet covers committing the changes, and the Docker Commands cheat sheet covers putting a Node app in a container.
Start a project
| Command | What it does |
|---|---|
| npm init | Make a package.json by answering questions |
| npm init -y | Make one with the defaults |
| npm -v | Which npm version you have |
| npm config list | Your npm settings |
Installing
| Command | What it does |
|---|---|
| npm install | Install everything in package.json (short: npm i) |
| npm install <package> | Add a package and save it to dependencies |
| npm install <package>@1.2.3 | Install an exact version (or a tag such as @latest) |
| npm install -D <package> | Add a development-only package (devDependencies) |
| npm install -g <package> | Install a command-line tool for your whole machine |
| npm ci | Clean install exactly what package-lock.json says (for CI and deploys) |
| npm install --omit=dev | Install without devDependencies |
| npm uninstall <package> | Remove a package |
| npm update | Update packages within the ranges in package.json |
| npm outdated | Show which packages have newer versions |
| npm ls <package> | Which versions are installed, and what pulled them in |
| npm explain <package> | Why a package is installed |
| npm dedupe | Simplify the tree by sharing packages where versions allow |
Running scripts
| Command | What it does |
|---|---|
| npm run | List the scripts in package.json |
| npm run <script> | Run a script, such as build or dev |
| npm start | Run the start script (no run needed) |
| npm test | Run the test script (short: npm t) |
| npm run <script> -- --watch | Pass extra options through to the script |
| npx <package> | Run a package's command without installing it for good |
| npx <package>@latest | Run the newest version |
Security and health
| Command | What it does |
|---|---|
| npm audit | List known vulnerabilities in your dependencies |
| npm audit fix | Update to fixed versions within your ranges |
| npm audit fix --force | Also allow major updates (can break things: test afterwards) |
| npm doctor | Check that npm, Node and the registry are working |
| npm cache verify | Check the cache and clear out bad data |
| npm cache clean --force | Empty the cache |
Package info
| Command | What it does |
|---|---|
| npm view <package> | A package's details from the registry |
| npm view <package> versions | Every published version |
| npm docs <package> | Open its documentation |
| npm repo <package> | Open its source repository |
| npm search <words> | Search the registry |
| npm fund | Which dependencies ask for funding |
Publishing
| Command | What it does |
|---|---|
| npm login | Sign in to the registry |
| npm whoami | Which account you're signed in as |
| npm version patch | Bump 1.2.3 to 1.2.4 and make a Git tag (minor and major work too) |
| npm pack --dry-run | List exactly which files would be published |
| npm publish | Publish the package |
| npm publish --access public | Publish a scoped package (@you/name) publicly |
| npm deprecate <package>@<version> "message" | Warn people away from a version |
| npm dist-tag add <package>@<version> beta | Point a tag such as beta at a version |
| npm link | Use a local copy of a package in another project while you work on it |
npm only lets you unpublish in limited cases (for example within 72 hours with no packages depending on it), so deprecate is usually the way to retire a version.
Common use cases
- Remember the difference between npm install, npm ci and npm update
- Fix vulnerabilities npm audit reports
- Publish or update your own package
- Learn npm for a JavaScript course or a new job
How to use
- Find the table for what you're doing, such as Installing or Publishing.
- Type a word in the filter, such as "audit" or "global".
- Copy the command and replace the parts in angle brackets.
- Check package.json and package-lock.json afterwards to see what changed.
FAQ
What's the difference between npm install and npm ci?
npm install resolves versions from package.json and may update package-lock.json. npm ci deletes node_modules and installs exactly what the lockfile lists, failing if the two files disagree, so every build gets the same packages.
What's the difference between dependencies and devDependencies?
dependencies are needed to run your code; devDependencies (installed with -D) are only needed to build or test it, such as test runners and bundlers. npm install --omit=dev skips them for production.
What does npx do?
It runs a command from a package, downloading the package to a cache if it isn't installed in your project. It's handy for one-off tools and project generators you don't want to install globally.
Should I run npm audit fix --force?
Only if you're ready to test. --force allows major version upgrades, which can change how a package works. Plain npm audit fix stays within the version ranges in package.json.
Related tools
- Git Commands: Cheat Sheet with ExamplesGit commands cheat sheet: set up, stage and commit, branch, merge and rebase, push and pull, history, stash…
- Docker Commands: Cheat Sheet with ExamplesDocker commands cheat sheet: build, run and stop containers, ports and volumes, logs and exec, networks…
- Linux Command CheatsheetCommon Linux terminal commands for file management and system administration.
- kubectl Commands: Kubernetes Cheat Sheetkubectl cheat sheet for Kubernetes: contexts and namespaces, get and describe, apply, scale and roll back…
- Mac Keyboard ShortcutsmacOS keyboard shortcuts for productivity and navigation.
- MIME Type LookupMIME type reference for content type headers and file handling.
- JSON ValidatorIn Utilities
- .gitignore GeneratorIn Utilities