npm Commands

npm commands cheatsheet.

Install, run, manage and publish — the npm commands you use every day. Tap to copy.

Installing
npm initCreate a package.json interactively
npm init -yCreate a package.json with defaults
npm installInstall all dependencies from package.json
npm install <pkg>Add a package as a dependency
npm install -D <pkg>Add a package as a dev dependency
npm install -g <pkg>Install a package globally
npm uninstall <pkg>Remove a package
Running scripts
npm startRun the start script
npm testRun the test script
npm run <script>Run a custom script from package.json
npm run buildRun the build script
npx <pkg>Run a package binary without installing it
npx serveServe the current folder as a static site
npx serve outServe a build output folder (e.g. Next.js export)
npx http-server -p 8080Quick local static server on a port
npx create-react-app my-appScaffold a project with a one-off tool
Managing
npm updateUpdate packages within allowed ranges
npm outdatedShow packages with newer versions available
npm list --depth=0List top-level installed packages
npm auditCheck dependencies for known vulnerabilities
npm audit fixAutomatically fix vulnerable dependencies
npm cache clean --forceClear the npm cache
Publishing
npm loginAuthenticate with the npm registry
npm version patchBump the patch version and tag it
npm publishPublish the package to the registry
npm packCreate a tarball without publishing

What npm actually does

npm is the package manager that comes with Node.js, and almost every JavaScript project leans on it for two jobs: pulling in code other people wrote, and running your project's tasks. The file at the centre of it all is package.json, which lists your dependencies and defines scripts. When you clone a project, a single npm install reads that file and downloads everything the project needs into a node_modules folder — the first command you'll run on almost any codebase.

Installing the right way

Adding packages has a few flavours worth understanding. npm install <pkg> adds a normal dependency your app needs to run. npm install -D <pkg> marks it as a dev dependency — tools like test runners and bundlers that you need while building but not in production. npm install -g <pkg> installs globally so a command-line tool is available everywhere on your machine. Getting these distinctions right keeps your project lean and your deployments clean.

Scripts and npx

The scripts section of package.json is where projects define their commands, run with npm run <name> (with the common ones, start and test, getting their own shortcuts: npm start and npm test). The companion command npx is the one people forget exists and then can't live without — it runs a package's executable without permanently installing it, which is perfect for one-off scaffolding tools like npx create-react-app or for trying something once.

Keeping things healthy

A few maintenance habits prevent trouble. npm outdated shows what has newer versions, npm update moves them forward within safe ranges, and npm audit flags known security vulnerabilities (with npm audit fix attempting to patch them automatically). If installs start behaving strangely, clearing the cache or deleting node_modules and reinstalling is the classic reset. These pair naturally with the Git commands for version control and the Docker commands when you containerise your app.

FAQ

What's the difference between npm install -D and -g?
-D (--save-dev) adds the package as a dev dependency for that project only; -g installs it globally on your machine so its command is available everywhere.
What does npx do?
npx runs a package's executable without permanently installing it — great for one-off tools like npx create-react-app.

More cheatsheets

yarn commands
Add, install & run scripts
pnpm commands
Fast, disk-efficient packages
Bun commands
install, run, test & bundle