Skip to content
Back to blog

npm vs npx: When to Install and When to Run

July 16, 20264 min read

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.

npmPackage managerInstall & manageSaves to node_modulesnpxPackage runnerExecute CLIsNo permanent install
npm installs · npx runs — same Node ecosystem, different jobs

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.

npm installnode_modulespackage.jsonnpm run
npm path: install → node_modules → import / npm run

Quick reference

  • Installs to node_modules (or a global prefix with -g).
  • Tracks versions in package.json and 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.

npx toolTemp fetchExecuteNo dep left
npx path: fetch if needed → run bin → leave project clean

Quick reference

  • Does not require a permanent global install.
  • Does not add the tool to your app’s node_modules as 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.

Intent?Keep in appnpm install lodashRun oncenpx create-…Pin CLInpx pkg@verProject scriptsnpm run …
Zoom: lodash as dependency vs create-app as one-shot CLI

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 -g tools 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.

11. Need import?Library in code → npm22. One-shot CLI?Scaffold / try → npx33. Team scripts?npm run in package.json1AvoidRandom global -g2Remembernpx ships with npm3Practicelodash vs create-app
Decision: manage with npm · execute with npx

Quick reference

  • Dependencies and scripts → npm.
  • Generators and one-off CLIs → npx.
  • CI: npm ci for installs; npx for 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.”

Key takeaway

Share:

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

Ask a new developer what "full-stack" means and you'll usually get two boxes: a frontend that renders pages, and a backe

Read

Roadmap graphics for "full stack developer" tend to list every technology that exists and imply you need all of it. You

Read

Git has hundreds of options, but day-to-day work clusters around a short list: set up a repo, stage and commit, talk to

Read

Keep learning

Follow a structured path or browse all courses to go deeper.