npm vs npx: When to Install and When to Run
npm installs and manages packages. npx runs a package (or CLI) without committing it to your project forever. Confusing them leads to global clutter, mystery binaries, or node_modules full of one-off scaffolding tools.
This quick guide compares purpose, install behavior, and everyday commands so you reach for the right tool. npx ships with npm 5.2+ — no separate install.
Running example: add lodash as a dependency with npm; scaffold a Next.js app once with npx create-next-app without keeping the scaffold CLI in your repo.
npm: install and manage packages
npm (Node Package Manager) is the default package manager for Node.js. It installs packages locally (project node_modules) or globally (-g), records them in package.json / lockfiles, and runs project scripts via npm run.
When to use: anything that should stay in the project — libraries, frameworks, and scripts your teammates need after npm install. When not to: one-shot CLIs you will never import — prefer npx so globals and deps stay clean.
Quick reference
- Installs to
node_modules(or a global prefix with-g). - Tracks versions in
package.jsonand the lockfile. - Common:
npm install,uninstall,update,npm run <script>. - Example:
npm install lodash— lodash becomes a project dependency.
Remember this
I use npm when a package must live in the project or as an intentional global tool.
npx: run without a permanent install
npx (Node Package eXecute) runs a package binary. If it is not already available, npx downloads it temporarily, executes it, and does not leave it as a normal project dependency. Perfect for generators and try-once CLIs.
When to use: scaffolding (create-next-app, create-react-app), one-off demos (cowsay), or pinning a CLI version (npx package@version). When not to: libraries your app imports at runtime — those belong in npm install.
Quick reference
- Does not require a permanent global install.
- Does not add the tool to your app’s
node_modulesas a dependency by default. - Common:
npx <pkg>,npx create-react-app my-app,npx pkg@version. - Example:
npx cowsay Hello— download, run, done.
Remember this
I use npx to execute CLIs and scaffolds without polluting globals or package.json.
Side-by-side: install vs execute
npm’s job is manage. npx’s job is run. npm says yes to installing and saving under node_modules. npx says no to permanent install for that one-shot — temporary fetch if needed, then execute.
Same ecosystem, different intent: after npm install lodash your code can import it forever; npx lodash is the wrong mental model for a library (lodash is not a typical CLI). Prefer npx for tools with a bin entry you want to invoke once.
Quick reference
- npm → install/manage; npx → execute.
- npm saves to node_modules; npx does not keep one-shot tools as deps.
- Both come together (npm 5.2+); no extra setup for npx.
- Global
-gtools still exist — prefer npx when you can.
Remember this
I can contrast npm (persist) vs npx (ephemeral run) in one sentence.
Decision rule and practice
Rule of thumb: npm when you want to install and manage packages; npx when you want to run a package or tool quickly without installing it. Pro tip: scaffolding and experimental CLIs are npx’s sweet spot — your disk and package.json stay cleaner.
Your homework: in an empty folder, run npm init -y, npm install lodash, then npx create-next-app@latest demo-app --yes (or equivalent). Confirm lodash is in package.json and the create tool is not.
Quick reference
- Dependencies and scripts → npm.
- Generators and one-off CLIs → npx.
- CI:
npm cifor installs;npxfor pinned tools without globals. - Prefer lockfiles; avoid random global installs on shared machines.
Remember this
I can choose npm vs npx from “keep it” vs “just run it.”
npm manages what your project owns. npx runs what you only need for a moment. Master that split and you spend less time cleaning global CLIs and mystery dependencies.
Remember: npx is already there once npm is installed — use it for scaffolds and experiments, and keep real libraries on the npm install path.
Related Articles
Explore this topic