npm Commands Cheat Sheet

The npm commands you will type most are npm install to add packages, npm ci for clean installs in CI, npm update to move within your version ranges, npm uninstall to remove a package, and npm run followed by a script name. Search the full table below, then test what a caret or tilde range allows.

The five you need first
TaskCommandChanges package.json?
Add a packagenpm install reactYes, adds it to dependencies
Add a dev toolnpm install -D viteYes, adds it to devDependencies
Install exactly what the lockfile saysnpm ciNever
Get newer versions inside your rangesnpm updateNo, unless you pass --save
Remove a packagenpm uninstall lodashYes, removes it

Updated

Command lists online tend to stop at the name of each command. This one adds the flags people actually reach for, the aliases, what each command writes to package.json and the lockfile, and a range tester so you can see which versions a caret, tilde or x-range really accepts.

Search npm commands

npm commands and flags
CommandGroupWhat it does
npm init -ySet upCreate a package.json with default answers.
npm installInstallInstall everything listed in package.json. Aliases include npm i and npm add.
npm install <pkg>InstallAdd a package to dependencies and the lockfile; saving is on by default.
npm install -D <pkg>InstallSave to devDependencies (long form --save-dev).
npm install -E <pkg>InstallSave an exact version instead of a caret range (--save-exact).
npm install -O <pkg>InstallSave to optionalDependencies (--save-optional).
npm install --no-save <pkg>InstallInstall without recording it in package.json.
npm install -g <pkg>InstallInstall globally, into npm's prefix folder rather than the project.
npm install --omit=devInstallSkip devDependencies, for example on a production server.
npm install --legacy-peer-depsInstallIgnore peer dependency conflicts the way older npm versions did.
npm ciInstallClean install from the lockfile: deletes node_modules, never writes package.json or the lockfile. Alias npm clean-install.
npm updateUpdateMove packages to the newest versions their ranges allow. Aliases up and upgrade.
npm update --saveUpdateSame, and write the new versions back to package.json.
npm update -gUpdateUpdate global packages to their latest tag.
npm install <pkg>@latestUpdateJump to the latest tag even across a major version.
npm outdatedUpdateShow Current, Wanted and Latest for each dependency.
npm uninstall <pkg>RemoveRemove a package and its entry in package.json and the lockfile. Aliases remove, rm, r, un and unlink.
npm uninstall --no-save <pkg>RemoveRemove the files but keep the entry in package.json.
npm uninstall -g <pkg>RemoveRemove a global package.
npm runScriptsList every script in package.json.
npm run devScriptsRun the dev script, usually a dev server.
npm run build -- --mode stagingScriptsPass flags after -- to the script instead of npm.
npm run lint --if-presentScriptsDo nothing, without failing, when the script does not exist.
npm startScriptsRun the start script; also works for test, stop and restart without run.
npm testScriptsRun pretest, test and posttest in order.
npx <pkg>ScriptsRun a package's binary without installing it globally.
npm auditSecurityAsk the registry for known vulnerabilities in your dependency tree.
npm audit fixSecurityApply fixes that stay inside your declared ranges.
npm audit fix --forceSecurityAllow fixes outside your ranges, including major versions. Review the changes.
npm audit --audit-level=highSecurityExit with an error only for high or critical findings, handy in CI.
npm lsInspectShow the installed dependency tree.
npm view <pkg> versionsInspectList published versions of a package.
npm config get prefixInspectShow the folder global packages install into.
npm run test -w aWorkspacesRun a script in one workspace (--workspace=a).
npm run test --workspacesWorkspacesRun a script in every workspace.

Semver range tester

Range ^1.2.3 expands to >=1.2.3 <2.0.0, so version 1.9.0 satisfies it.

Semver ranges explained

What each range expands to (node-semver rules, used by npm)
RangeMeansAllows
^1.2.3>=1.2.3 <2.0.0Minor and patch updates
^0.2.3>=0.2.3 <0.3.0Patch updates only
^0.0.3>=0.0.3 <0.0.4Nothing newer
~1.2.3>=1.2.3 <1.3.0Patch updates
~1>=1.0.0 <2.0.0Same as 1.x
1.2.x>=1.2.0 <1.3.0Any patch of 1.2
*>=0.0.0Any version
1.2.3 - 2.3.4>=1.2.3 <=2.3.4Inclusive span

package.json scripts

A typical scripts block

prebuild runs automatically before build. Local binaries such as vite resolve from node_modules/.bin.

{
  "scripts": {
    "dev": "vite",
    "prebuild": "npm run lint",
    "build": "vite build",
    "preview": "vite preview",
    "lint": "eslint .",
    "test": "vitest run"
  }
}

Install step for CI

Clean, repeatable install, a build, then a security gate that only fails on serious findings.

npm ci
npm run build
npm audit --omit=dev --audit-level=high

Install, ci and update do different jobs

Use npm install while developing. It adds packages, saves them to package.json by default and keeps the lockfile in step with what you asked for.

Use npm ci on build servers and in Docker images. It starts from an empty node_modules and treats the lockfile as the source of truth, so every build gets the same tree.

Use npm update when you want bug fixes without touching your declared ranges. Check npm outdated first so you know what will move.

Scripts are shortcuts with a few rules

Anything in the scripts object of package.json runs with npm run name. npm adds node_modules/.bin to the PATH for the script, which is why vite or next work there without a global install.

A script called prebuild runs before build and postbuild runs after it. test, start, stop and restart are special enough that you can skip the word run for them.

How it works

  1. Type a word such as save, global or lock into the search box, or pick a category, to narrow the command table.
  2. Use the copy button on any row to put the command on your clipboard.
  3. In the semver tester, enter a range and a version to see whether the version satisfies it and what the range expands to.
  4. Read the scripts section for how npm run, pre and post scripts and the -- separator behave.

Tips

Frequently asked questions

What is the difference between npm install and npm ci?

npm install resolves versions from package.json, may update the lockfile, and keeps the existing node_modules. npm ci needs a lockfile, deletes node_modules first, installs exactly what the lockfile lists, fails if it disagrees with package.json, and never writes either file.

Does npm update change package.json?

Not by default. The npm docs list save as false for npm update, so only node_modules and the lockfile change. Pass --save if you want the new versions written back to package.json.

What does the caret in ^1.2.3 mean?

It allows any change that keeps the left-most non-zero number the same. ^1.2.3 accepts 1.x from 1.2.3 up but not 2.0.0. Below 1.0.0 it is stricter: ^0.2.3 only accepts 0.2.x patches.

How do I pass a flag to the script behind npm run dev?

Put -- before it, for example npm run dev -- --port 4000. Everything after the two dashes goes to the script instead of being read by npm.

What do Wanted and Latest mean in npm outdated?

Wanted is the highest version your package.json range allows. Latest is the version the package author tagged as latest on the registry. When Latest is higher than Wanted, getting it means editing the range.

Related: pnpm vs npm, Install Node.js and npm on Mac, Git cheat sheet, JSON formatter for package.json, Regex cheat sheet.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap