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

CommandWhat it does
npm initMake a package.json by answering questions
npm init -yMake one with the defaults
npm -vWhich npm version you have
npm config listYour npm settings

Installing

CommandWhat it does
npm installInstall everything in package.json (short: npm i)
npm install <package>Add a package and save it to dependencies
npm install <package>@1.2.3Install 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 ciClean install exactly what package-lock.json says (for CI and deploys)
npm install --omit=devInstall without devDependencies
npm uninstall <package>Remove a package
npm updateUpdate packages within the ranges in package.json
npm outdatedShow 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 dedupeSimplify the tree by sharing packages where versions allow

Running scripts

CommandWhat it does
npm runList the scripts in package.json
npm run <script>Run a script, such as build or dev
npm startRun the start script (no run needed)
npm testRun the test script (short: npm t)
npm run <script> -- --watchPass extra options through to the script
npx <package>Run a package's command without installing it for good
npx <package>@latestRun the newest version

Security and health

CommandWhat it does
npm auditList known vulnerabilities in your dependencies
npm audit fixUpdate to fixed versions within your ranges
npm audit fix --forceAlso allow major updates (can break things: test afterwards)
npm doctorCheck that npm, Node and the registry are working
npm cache verifyCheck the cache and clear out bad data
npm cache clean --forceEmpty the cache

Package info

CommandWhat it does
npm view <package>A package's details from the registry
npm view <package> versionsEvery published version
npm docs <package>Open its documentation
npm repo <package>Open its source repository
npm search <words>Search the registry
npm fundWhich dependencies ask for funding

Publishing

CommandWhat it does
npm loginSign in to the registry
npm whoamiWhich account you're signed in as
npm version patchBump 1.2.3 to 1.2.4 and make a Git tag (minor and major work too)
npm pack --dry-runList exactly which files would be published
npm publishPublish the package
npm publish --access publicPublish a scoped package (@you/name) publicly
npm deprecate <package>@<version> "message"Warn people away from a version
npm dist-tag add <package>@<version> betaPoint a tag such as beta at a version
npm linkUse 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

  1. Find the table for what you're doing, such as Installing or Publishing.
  2. Type a word in the filter, such as "audit" or "global".
  3. Copy the command and replace the parts in angle brackets.
  4. 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.