Tools › Cheat sheets › Programming & Tooling › npm
📗

npm cheat sheet

npm is the package manager that comes with Node.js. Dependencies are listed in package.json; package-lock.json pins exact versions.

Click any command to copy it.

Project setup

CommandWhat it does
npm init -yCreate a package.json with defaults.
node -v && npm -vShow Node and npm versions.
npm installInstall all dependencies from package.json.
npm ciClean, exact install from the lockfile (best for CI).
npm install --omit=devInstall only production dependencies.

Add and remove packages

CommandWhat it does
npm install packageAdd a dependency.
npm install -D packageAdd a development dependency.
npm install package@1.2.3Install an exact version.
npm install package@latestInstall the newest version.
npm install -g packageInstall globally.
npm uninstall packageRemove a dependency.
npm ls -g --depth=0List globally installed packages.
npm pruneRemove packages that are not listed in package.json.

Scripts

CommandWhat it does
npm run buildRun the build script from package.json.
npm startRun the start script.
npm testRun the test script.
npm runList available scripts.
npx packageRun a package's command without installing it globally.
npm run build -- --flagPass arguments through to the script.

Inspect and update

CommandWhat it does
npm ls --depth=0List top-level installed packages.
npm outdatedShow packages with newer versions.
npm updateUpdate within the allowed version ranges.
npm view package versionShow a package's latest version.
npm explain packageExplain why a package is installed.
npm doctorCheck your npm environment for problems.
npm config get registryShow the registry in use.

Security

CommandWhat it does
npm auditReport known vulnerabilities in dependencies.
npm audit fixApply compatible fixes automatically.
npm audit fix --forceDestructive: Apply fixes that may include breaking major upgrades.
npm audit --omit=devAudit only production dependencies.

Publish and clean up

CommandWhat it does
npm loginLog in to the registry.
npm whoamiShow the logged-in user.
npm version patchBump the patch version and create a git tag.
npm packCreate the tarball that would be published, to inspect it.
npm publishPublish the package (use --access public for scoped public packages).
npm cache clean --forceClear the npm cache.
rm -rf node_modules && npm ciReinstall from scratch to fix odd dependency problems.

Frequently asked questions

What is the difference between npm install and npm ci?

npm install can update the lockfile; npm ci installs exactly what the lockfile says and fails if it does not match package.json, which makes builds reproducible.

What does -D do?

-D (--save-dev) records the package under devDependencies, which are not installed when you use --omit=dev in production.

More Programming & Tooling cheat sheets

🐍 Python (venv & pip)🧩 Regular expressionsAll Programming & Tooling cheat sheets →
Copied to clipboard