All cheatsheetsFrontend Development

npm

npm 10+

Quick reference for everyday npm CLI commands: init / install / add / remove / update / scripts / run / publish / audit, plus lockfile, registry, and cache management — with common options and practical examples.

34 commands

Project init

npm init

Walk through `package.json` creation interactively, prompting for name / version / description / entry point / etc.

-y accept all defaults; --init.author 'Name <e@x>' override defaults

npm init -y
npm init -y

Generate a `package.json` immediately with sensible defaults (`name` from directory, `version` 1.0.0, etc.) — the modern starting point.

npm init -y && cat package.json

Install

npm install

Install every dependency listed in `package.json`, refreshing `node_modules` and `package-lock.json`. Skips dev deps if `NODE_ENV=production`.

--save-exact / -E pin exact versions; --legacy-peer-deps; --force; --no-audit; --prefer-offline

npm install
npm ci

Clean install strictly from `package-lock.json`. Faster than `npm install`, deletes `node_modules` first, and refuses to mutate the lockfile — the right call for CI.

--omit=dev skip devDependencies; --prefer-offline

npm ci --omit=dev

Add dependency

npm install <pkg>

Add a runtime dependency (default `dependencies`). Records the resolved version in `package-lock.json` and writes `package.json`.

<pkg>@<version-range>; --save-dev / -D; --save-exact / -E; --save-optional / -O; --save-prod / -P

npm install lodash && npm install eslint@^9 --save-dev
npm install <pkg>@<version>

Install a specific version range (semver): exact `1.2.3`, tilde `~1.2.3`, caret `^1.2.3`, or tag like `latest` / `next` / `beta`.

npm install [email protected] && npm install next@latest
npm install -D <pkg>

Add to `devDependencies` — tools only needed at build / test time (TypeScript, Jest, ESLint, Prettier).

npm install -D typescript @types/node vitest
npm install -g <pkg>

Install a package globally so its binary is on `PATH` for every project (TypeScript, tsx, create-react-app, http-server).

npm install -g typescript tsx pnpm

Remove

npm uninstall <pkg>

Remove a package from `node_modules` and `package.json`.

-D remove from devDependencies; -g remove a global; --save false to skip manifest edit (rare)

npm uninstall lodash && npm uninstall -D @types/node

Update

npm update

Upgrade every dependency within the range allowed by `package.json` semver specs, refreshing `package-lock.json` accordingly.

--save false to skip manifest write; --workspace update inside workspaces

npm update
npm update <pkg>

Update one package to the latest version that satisfies its current semver range.

npm update eslint
npm install <pkg>@latest

Upgrade a single package to the newest release (bypasses the semver range and rewrites the range to `^<new-version>`).

npm install typescript@latest && npm install -D vitest@latest

Run script

npm run <script>

Execute a script defined under the `scripts` section of `package.json`. `npm test`, `npm start`, `npm stop`, `npm restart` are shorthand for `npm run X`.

-- silent suppress npm log noise; --if-present don't error if missing; -- <args> forward remaining args to the script

npm run build -- --mode production
npm run <script> -- <args>

Pass extra CLI arguments to the underlying script. Everything after `--` is forwarded untouched.

npm run lint -- --fix src/
npm exec -- <command>

Run a command from a local or `npx`-cached package. Replaces the deprecated `npx <command>` direct call.

npm exec -- create-react-app my-app

Inspect

npm ls

Print the dependency tree of the current project (top-level + transitive). Useful for finding duplicate copies of a package.

--all; --depth=N limit depth; --json output JSON; -g list globals

npm ls --depth=0 && npm ls react
npm outdated

Show a table of every dep whose installed version is older than the latest version allowed by the manifest's range.

--long verbose; --json output JSON

npm outdated
npm view <pkg>

Display registry metadata for a package — versions, dist-tags, dependencies, maintainers, repository URL, readme excerpt.

versions; time; dist-tags; repository.url

npm view lodash versions && npm view react dist-tags

Lockfile

npm shrinkwrap

Generate a standalone `npm-shrinkwrap.json` (same format as `package-lock.json`) for apps that need reproducible installs even when shipped without a lockfile.

npm shrinkwrap

Registry

npm config get registry

Print the currently-active registry URL. Default is `https://registry.npmjs.org/`.

npm config get registry
npm config set registry <url>

Point npm at a private / cached registry (GitHub Packages, Verdaccio, China mirrors). Write to `~/.npmrc` or project `.npmrc`.

npm config set registry https://registry.npmmirror.com/
npm login

Authenticate against the configured registry and store a token in `~/.npmrc`. Required before `npm publish`.

--registry=<url> log in to a different registry; --scope=<scope> restrict token

npm login --registry=https://registry.npmjs.org/

Publish

npm publish

Upload the current package to the configured registry. Requires `name` (and usually `version`, `repository`) in `package.json` and a logged-in user with publish rights.

--access public|restricted (scoped packages); --dry-run; --tag <tag> ship under a dist-tag

npm publish --access public --tag beta
npm unpublish <pkg>@<version>

Remove a specific version from the registry. **Strongly discouraged within 72 hours of publish** — npm supports it but discourages.

npm unpublish [email protected]
npm deprecate <pkg>@<version> '<msg>'

Mark a version as deprecated with a message that npm prints on install. The right way to point users at a fix without deleting history.

npm deprecate [email protected] 'use 1.2.4+ — security fix'

Audit

npm audit

Run a security audit against the installed dependency tree, fetching advisories from the npm registry.

--json; --omit=dev skip dev deps; --audit-level=low|moderate|high|critical

npm audit --omit=dev
npm audit fix

Auto-upgrade packages to the closest non-breaking version that resolves the advisory. Use `npm audit fix --force` only when you accept semver-major jumps.

--force; --dry-run

npm audit fix

Cache

npm cache clean --force

Wipe the global npm cache (`~/.npm` on POSIX). Use after switching registries or chasing weird install errors.

npm cache clean --force
npm cache verify

Validate the integrity of cached tarballs without deleting. Safe to run any time.

npm cache verify

Workspaces

npm install -w <workspace>

Install a package into a single named workspace within a monorepo (defined under `workspaces` in `package.json`).

npm install lodash -w @myorg/web
npm run <script> -ws

Run a script in every workspace of the monorepo. `-w <name>` runs only one workspace.

--if-present don't error if missing

npm run build -ws && npm test -w @myorg/api

Misc

npm doctor

Run environment diagnostics (Node version, registry reachability, cache health, Git presence) — handy when troubleshooting.

npm doctor
npm fund

Print the funding links for the current project's direct dependencies (used by maintainers to sponsor OSS).

npm fund
npm pkg set <key>=<value>

Mutate fields inside `package.json` from the CLI without opening an editor. Useful in scripts / hooks.

npm pkg set scripts.lint='eslint .' && npm pkg get version

Related command cheatsheets

About npm

npm is the default package manager for Node.js, originally written by Isaac Z. Schlueter in 2010 as a standalone tool, and bundled with Node since Node.js 0.6.3 in 2011. The name stood for `Node Package Manager` but the team has officially stopped treating it as an acronym since 2014. The current stable line is npm 10, which ships alongside Node 20+. npm talks to a registry (the default `registry.npmjs.org`, a public CouchDB hosted by GitHub since 2020), reads / writes `package.json` + `package-lock.json`, and supports both local installs (`node_modules` in your project) and global installs (added to PATH). npm 7+ introduced workspaces for monorepos; npm 9+ ships with `npx` built in and a strict peer-dependency engine. Most npm commands accept `-D` (dev), `-E` (exact), `-g` (global), `--save-prod`, and a `--prefix` flag for non-standard install directories. npm never uploads your code — it only writes to local files and the registry. Anonymous download metrics are aggregated by the npm registry but no project contents are sent.

Cheatsheet version 1.0.0