cheat-sheets

Node.js and npm

Node.js runs JavaScript outside the browser; npm is its bundled package manager and pnpm is a faster, disk-efficient alternative. Manage Node versions with nvm or fnm rather than a system package so each project can pin its own.

Documentation: npm docs, pnpm docs, fnm, nvm

Version Managers #

COMMANDDESCRIPTION
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bashInstall nvm (check the latest tag; review the script first)
nvm install --ltsInstall the latest LTS release
nvm install 22Install a major version
nvm use 22Use a version in this shell
nvm alias default 22Default for new shells
nvm ls / nvm ls-remote --ltsInstalled / available versions
nvm useUse the version from .nvmrc in the current directory
curl -fsSL https://fnm.vercel.app/install | bashInstall fnm (fast, Rust based; also brew install fnm, winget install Schniz.fnm)
eval "$(fnm env --use-on-cd)"Enable fnm in bash/zsh with automatic version switching (put in the shell profile)
fnm install --lts / fnm use 22 / fnm default 22Install, use and set default
fnm list / fnm list-remoteInstalled / available versions
node -v / npm -vCheck versions

Pin a project version by putting 22 (or lts/*) in .nvmrc; fnm reads .nvmrc and .node-version. The engines field in package.json documents the supported range. corepack enable activates pnpm and yarn at the version a project declares in packageManager (Corepack ships with most Node releases; newer ones may need npm install -g corepack first).

npm Commands #

COMMANDDESCRIPTION
npm init -yCreate a package.json
npm install / npm iInstall everything from package.json and the lockfile
npm ciClean install strictly from package-lock.json (CI, fails on mismatch)
npm install <PKG>Add a dependency
npm install -D <PKG>Add a dev dependency
npm install <PKG>@<VERSION>Specific version or tag (@latest, @next)
npm install -g <PKG>Global install (avoid; prefer npx or a version manager)
npm uninstall <PKG>Remove a dependency
npm updateUpdate within the semver ranges
npm outdatedShow packages with newer versions
npm ls / npm ls --depth=0Dependency tree / top level only
npm ls <PKG>Why is this package installed
npm view <PKG> versionsPublished versions
npm run <SCRIPT>Run a scripts entry (npm test, npm start omit run)
npm run <SCRIPT> -- --flagPass arguments through to the script
npm exec <CMD> / npx <CMD>Run a local or remote package binary
npm config get registryShow the registry in use
npm cache verifyCheck the cache (npm cache clean --force clears it)
npm pack --dry-runSee what would be published
npm publish --access publicPublish a package (npm login first)
npm linkSymlink a local package for development
npm prefix -gGlobal install location

pnpm Commands #

COMMANDDESCRIPTION
pnpm installInstall dependencies (--frozen-lockfile for CI)
pnpm add <PKG> / pnpm add -D <PKG>Add a dependency / dev dependency
pnpm remove <PKG>Remove a dependency
pnpm update / pnpm update --latestUpdate within ranges / to latest
pnpm outdatedOutdated packages
pnpm run <SCRIPT> / pnpm <SCRIPT>Run a script
pnpm dlx <PKG>Run a package once, like npx
pnpm why <PKG>Why is this installed
pnpm store pruneRemove unreferenced packages from the global store
pnpm -r <CMD>Run in every workspace package
pnpm --filter <NAME> <CMD>Run in one workspace package

pnpm uses a shared content-addressable store and hard links, which saves disk space and keeps node_modules strict about undeclared dependencies.

npx #

COMMANDDESCRIPTION
npx <PKG>Run a package binary, downloading it if needed
npx <PKG>@<VERSION>Run a specific version
npx --no-install <CMD>Only use a locally installed binary
npx -y create-vite@latest my-appScaffold a project without the confirmation prompt
npx http-server -p 8080Throwaway static server
npx serve .Another static server

Only run packages you trust: npx executes code straight from the registry.

package.json #

json
{
  "name": "my-app",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "engines": { "node": ">=22" },
  "packageManager": "pnpm@9.15.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "test": "vitest run",
    "lint": "eslint .",
    "prebuild": "npm run lint",
    "postinstall": "echo done"
  },
  "dependencies": {},
  "devDependencies": {}
}
FIELDMEANING
scriptsNamed commands; pre<NAME> and post<NAME> run automatically around <NAME>
dependenciesNeeded at runtime
devDependenciesBuild and test tooling only
type: "module"Treat .js as ES modules
enginesDeclares supported Node versions (enforced only with engine-strict)
binExecutables a package installs
workspacesMonorepo package globs (npm and yarn; pnpm uses pnpm-workspace.yaml)
overridesForce a version of a transitive dependency (npm; pnpm uses pnpm.overrides)

Version ranges: ^1.2.3 allows 1.x.x (below 2.0.0), ~1.2.3 allows 1.2.x, 1.2.3 is exact.

Lockfiles #

MANAGERLOCKFILECI INSTALL
npmpackage-lock.jsonnpm ci
pnpmpnpm-lock.yamlpnpm install --frozen-lockfile
yarnyarn.lockyarn install --immutable (Yarn 2+)

Commit the lockfile for applications and use one package manager per project. Delete node_modules and run the CI install command to reproduce a clean build.

Audit and Security #

COMMANDDESCRIPTION
npm auditReport known vulnerabilities in dependencies
npm audit --omit=devIgnore dev dependencies
npm audit fixApply compatible fixes (--force allows breaking upgrades; review first)
npm audit signaturesVerify registry signatures and provenance
pnpm auditSame check with pnpm
npm ci --ignore-scriptsInstall without running lifecycle scripts from dependencies
npm config set ignore-scripts trueDisable install scripts by default

Troubleshooting #

SYMPTOMFIX
EACCES on global installUse nvm/fnm (user-owned Node) instead of sudo npm -g
ERESOLVE peer dependency conflictCheck npm ls; last resort --legacy-peer-deps
Stale or odd installrm -rf node_modules then npm ci
Wrong Node versionnode -v, then nvm use / fnm use; check .nvmrc
Behind a proxy or private registrynpm config set registry <URL> or a project .npmrc
Port already in usess -ltnp | grep <PORT> (Linux Basics)

Never commit an .npmrc that holds an auth token; use environment variables (//registry.npmjs.org/:_authToken=${NPM_TOKEN}).

Not Verified #

Commands were written from the official documentation and not executed. The nvm install script version shown may be out of date.

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.