Skip to content

Node.js & npm Cheat Sheet

Node CLI, npm, and package.json essentials.

A quick reference for the Node.js runtime and npm — the CLI flags, package commands, and `package.json` fields that come up when you set up or maintain a project.

The semver section is the one people misread: `^1.2.3` and `~1.2.3` allow different ranges of updates, and knowing which is which is the difference between a safe `npm update` and a surprise breaking change.

Node CLI

node file.jsRun a script.
node --watch file.jsRe-run on file changes (Node 18.11+).
node --env-file=.env file.jsLoad env vars from a file (Node 20.6+).
node --inspect file.jsStart with the debugger attached.
node -v · node -e "code"Print version; run inline code.

npm — install & run

npm install · npm ciInstall deps; ci does a clean, lockfile-exact install.
npm install pkg · npm i -D pkgAdd a dependency; -D for devDependencies.
npm uninstall pkgRemove a dependency.
npm run <script> · npm startRun a package.json script.
npm outdated · npm updateList outdated deps; update within ranges.
npx <pkg>Run a package binary without installing globally.

package.json fields

"type": "module"Treat .js files as ES modules (import/export).
"scripts": { "dev": "..." }Named commands run via npm run.
"engines": { "node": ">=20" }Declare the required Node version.
"main" · "exports"Entry point; exports defines the public API map.
"dependencies" vs "devDependencies"Runtime deps vs build/test-only deps.

Semver ranges

How a version range in package.json is interpreted.

^1.2.3Allows 1.x.x up to <2.0.0 (minor + patch).
~1.2.3Allows 1.2.x up to <1.3.0 (patch only).
1.2.3Exact version only.
>=1.2.0 <2.0.0Explicit range.
* · latestAny version — avoid in libraries.

Frequently asked questions

What is the difference between npm install and npm ci?

`npm install` resolves dependencies and may update `package-lock.json`. `npm ci` does a clean install strictly from the lockfile — it deletes `node_modules` first and fails if the lockfile and `package.json` disagree. Use `npm ci` in CI and deployments for reproducible builds.

What does the caret (^) mean in package.json versions?

`^1.2.3` allows any release compatible with 1.2.3 under semver — that is, up to but not including 2.0.0, so new minor and patch versions are accepted. `~1.2.3` is stricter, allowing only patch updates (up to <1.3.0).