Coding

npm vs npx: What's the Difference and When to Use Each

npm is the package manager: it installs, updates and removes packages and runs the scripts in package.json. npx runs a command from a package, using the copy in your project's node_modules/.bin if there is one, and otherwise downloading the package into the npm cache just for that run, without adding it to your project.

In short: npm install -D prettier adds Prettier to the project; npx prettier --check . runs it.

npm vs npx at a glance

npmnpx
Main jobManage dependencies (install, update, remove, publish)Execute a package's binary
Changes package.jsonYes, on npm install <pkg>No
Where the package ends upnode_modules (or the global folder with -g)Local node_modules/.bin if present, otherwise a folder in the npm cache
Typical commandnpm install, npm run buildnpx eslint ., npx create-vite my-app
Ships withNode.jsnpm (it is npm exec underneath)

What npx actually does

The npm docs describe npx as a command that runs "an arbitrary command from an npm package (either one installed locally, or fetched remotely)". Since npm 7 it no longer has its own installer; npx "uses the npm exec command instead of a separate argument parser and install process". When you run npx something, npm:

  1. Looks for the command in the project's local dependencies. Packages you installed put their executables in node_modules/.bin.
  2. If it is not there, prompts before installing the package into a folder in the npm cache. You can skip the prompt with --yes (answer yes) or --no (refuse and fail). The npm exec docs add that if standard input is not a terminal, or a CI environment is detected, --yes is assumed.
  3. Adds that cache folder to PATH for this one process and runs the command.

Nothing is written to package.json or your project's node_modules in step 2. On a Windows machine you can see the cached installs under %LocalAppData%\npm-cache\_npx, one folder per package set with a hash name.

Running a locally installed tool

This is the most common use. After npm install --save-dev [email protected], the project's node_modules/.bin on Windows contains:

cowsay
cowsay.cmd
cowsay.ps1
cowthink
cowthink.cmd
cowthink.ps1

Those are shims for the shell, Command Prompt and PowerShell. Typing cowsay in a terminal fails because node_modules/.bin is not on your PATH. npx finds it:

npx cowsay --version
1.6.0

npm scripts get the same benefit without npx. The npm scripts docs say dependency executables "will be added to the PATH for executing the scripts", so this works:

{
  "scripts": { "format": "prettier --write ." },
  "devDependencies": { "prettier": "3.3.3" }
}
npm run format

For tools your project depends on, a script is usually better than telling people to type an npx command: the version is pinned in package.json, and npm ci installs exactly that version from the lockfile.

Running a tool you haven't installed

npx --yes [email protected] --version
3.3.3

That downloaded Prettier 3.3.3 into the npm cache, ran it and left the project untouched. This is the right shape for one-off commands and project generators, such as npx create-vite my-app, where installing the generator permanently would be pointless.

Without a version, npx resolves the package name to the latest release at the time you run it. In scripts and CI, always pin a version ([email protected]) so a new release can't change behaviour, or worse, run code you have never looked at.

When the command name differs from the package name

npx guesses the binary from the package name: if the package has one binary, it runs that; if it has several, it picks the one matching the package name. When neither works, name the package with --package (or -p) and give the command after --:

npx --yes [email protected] -- cowsay --version
1.5.0

Note that -p means --package only in npx. In npm it is short for --parseable, a difference the npx docs call out.

npx flags and argument gotchas

The npx docs say that when you use the npx binary, "all flags and options must be set prior to any positional arguments". Anything after the package name goes to the tool you are running. Put npx's own flags first, and use -- when in doubt to mark where npm should stop parsing.

One case behaved unexpectedly when tested with npm 9.8.1 on Windows: npx --no cowsay --version printed npm's version (9.8.1) instead of cowsay's, while npx --no -- cowsay --version printed 1.6.0. With a package that was not installed, the -- form refused cleanly:

npx --no -- prettier --version
npm ERR! npx canceled due to missing packages and no YES option: ["[email protected]"]

That is the behaviour you want in CI if a step must only use tools from the lockfile: --no turns a surprise download into a failure.

What changed from the old npx

If you learned npx before npm 7, a few options are gone. The npx docs list these changes:

  • --ignore-existing is removed; local binaries are always on the PATH.
  • --npm is removed.
  • Shell fallback (running a command when no package matched) is removed.
  • --no-install is deprecated and converted to --no.
  • npx now prompts before installing anything; -y or --yes suppresses it.

npx vs npm exec

They run the same code. npm exec (alias npm x) is the npm subcommand; npx is the shortcut binary. The only practical difference is argument parsing: with npm exec, npm parses options until it sees --, so the documented form is npm exec -- <pkg> [args]. These are equivalent:

npx eslint --fix .
npm exec -- eslint --fix .

Which one to use

  • Adding, removing or updating a dependency: npm (npm install, npm uninstall, npm update).
  • Running a tool the project already depends on: an npm script, or npx <tool> for ad-hoc runs.
  • Running a one-off generator or utility: npx <pkg>@<version>.
  • Running a tool you use across every project: either npm install -g or npx. Global installs can drift out of date and differ between machines; npx with a pinned version does not.

If an npm install fails with a peer dependency conflict along the way, see fixing npm ERESOLVE. If a tool you run with npx reads settings from the environment, setting environment variables covers each shell.

Frequently asked questions

Do I need to install npx separately?
No. npx ships with npm, and since npm 7 it is a thin front end for npm exec. If npm --version works, npx --version should print the same version number.
Where does npx put packages it downloads?
Into a folder inside the npm cache, not into your project. That folder is added to PATH only for the command being run. On Windows the cache is under %LocalAppData%\npm-cache, and npx's folders sit in its _npx subfolder. Your package.json and node_modules are not changed.
Is npx slower than a global install?
The first run of a package that isn't installed locally downloads it, which takes a few seconds. Later runs reuse the cached copy. A tool you run many times a day in a project is better added as a devDependency, so npx or an npm script finds it in node_modules/.bin without any download.
Is it safe to run npx with a package name I found online?
npx runs code from the registry with your user's permissions, including the package's install scripts. Check the exact package name for typos, prefer well-known packages, and pin a version (npx [email protected]) for anything in scripts or CI. The prompt npx shows before installing is your chance to check the name.

Sources