Back to All Cheatsheet Libraries cheatsheets

npm & Node.js

npm command reference, package.json fields and version semantics, and a dependency-hygiene workflow.

Total Commands: 0
Category Command Description
Installnpm installInstalls every dependency listed in package.json.
Installnpm install package-nameInstalls a package and adds it to dependencies.
Installnpm install -D package-nameInstalls as a devDependency — not shipped in a production install.
Installnpm ciClean install strictly from package-lock.json — faster and reproducible, the standard choice for CI.
Removenpm uninstall package-nameRemoves a package and its entry from package.json.
Updatenpm outdatedLists installed packages with newer versions available.
Updatenpm updateUpdates packages within the ranges allowed by package.json.
Securitynpm auditScans installed dependencies for known vulnerabilities.
Securitynpm audit fixAttempts to automatically upgrade vulnerable dependencies to a patched version.
Scriptsnpm run script-nameRuns a named script defined in package.json's "scripts" field.
Runnpx package-nameRuns a package's binary without a permanent global install.
Globalnpm install -g package-nameInstalls a package globally, making its CLI available system-wide.
Publishnpm publishPublishes the current package to the npm registry.
Versioningnpm version patchBumps the patch version in package.json and creates a matching git tag.

package.json Fields

Field Purpose
"main"The entry file loaded when the package is required/imported.
"type": "module"Treats .js files as ES modules (import/export) instead of CommonJS (require).
"scripts"Named shell commands runnable via npm run — build, test, dev, lint, etc.
"dependencies"Packages required at runtime — installed in a production install.
"devDependencies"Packages only needed for development/build/test — skipped by npm ci --omit=dev.
"engines"Declares which Node.js version(s) the package supports.

Version Semantics

^1.2.3

Allows 1.x.x, not 2.0.0

~1.2.3

Allows 1.2.x only

1.2.3

Exact version, no range

package-lock.json

Exact resolved tree, always commit it

Keeping Dependencies Healthy

A routine that catches dependency problems before they become emergencies.

1

Always commit package-lock.json

Guarantees every install (local, CI, teammate) resolves to the exact same dependency tree — without it, a "^" range can silently pull a different minor version on different machines.

2

Use npm ci in CI/CD, not npm install

npm ci refuses to run if package.json and the lockfile disagree, and is faster since it skips dependency resolution — install for local dev, ci for anything automated.

3

Run npm audit on a schedule

New vulnerabilities are disclosed against already-installed packages constantly — a periodic audit catches these even if dependencies themselves haven't changed.

4

Update deliberately, not reflexively

npm outdated first, then update in small batches with tests run after each — a mass "update everything" is much harder to bisect when something breaks.

Quick Tips

npx runs a one-off tool without installing it globally
npx create-vite@latest always fetches the latest version — avoids the "my global CLI is out of date" class of bugs entirely.
nvm manages multiple Node versions
Different projects often need different Node versions — nvm (or fnm/volta) switches per-project via a .nvmrc file instead of one global system install.
node_modules should never be committed
Regenerable from package-lock.json on any machine — committing it bloats the repo and causes cross-platform binary mismatches (native modules compiled for the wrong OS).