npm commands cheatsheet.
Install, run, manage and publish — the npm commands you use every day. Tap to copy.
npm initCreate a package.json interactivelynpm init -yCreate a package.json with defaultsnpm installInstall all dependencies from package.jsonnpm install <pkg>Add a package as a dependencynpm install -D <pkg>Add a package as a dev dependencynpm install -g <pkg>Install a package globallynpm uninstall <pkg>Remove a packagenpm startRun the start scriptnpm testRun the test scriptnpm run <script>Run a custom script from package.jsonnpm run buildRun the build scriptnpx <pkg>Run a package binary without installing itnpx serveServe the current folder as a static sitenpx serve outServe a build output folder (e.g. Next.js export)npx http-server -p 8080Quick local static server on a portnpx create-react-app my-appScaffold a project with a one-off toolnpm updateUpdate packages within allowed rangesnpm outdatedShow packages with newer versions availablenpm list --depth=0List top-level installed packagesnpm auditCheck dependencies for known vulnerabilitiesnpm audit fixAutomatically fix vulnerable dependenciesnpm cache clean --forceClear the npm cachenpm loginAuthenticate with the npm registrynpm version patchBump the patch version and tag itnpm publishPublish the package to the registrynpm packCreate a tarball without publishingWhat 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.