Before npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. existed, using a project scaffolder like create-react-app meant installing it globally with npm install -g, leaving a permanent entry on your machine that would slowly fall out of date. Run npx create-react-app my-app today and you get the latest version every single time, with nothing left behind when you are done.
The problem npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. solves
Global installs have three annoying failure modes. First, the globally installed version gets stale, you ran npm install -g create-react-app six months ago and now the scaffolder generates a project with last year's defaults. Second, different projects on the same machine might need different versions of the same CLIWhat is cli?Short for Command Line Interface. A tool you use by typing commands in the terminal instead of clicking buttons., and a global install forces you to pick one. Third, every global package is just noise on your system PATH.
npx fixes all three by following this resolution order:
- Look for the binaryWhat is binary?A ready-to-run file produced by the compiler. You can send it to any computer and it just works - no install needed. in
./node_modules/.bin(the local project version) - If not found, download and run it temporarily from the npm registryWhat is registry?A server that stores and distributes packages or container images - npm registry for JavaScript packages, Docker Hub for container images.
- Clean up after execution
node_modules/.bin first, running npx tsc in a project that has TypeScript installed as a devDependency will always use that project's exact version, not whatever is on your PATH.Common use cases
ScaffoldingWhat is scaffolding?Auto-generating the basic file structure and starter code for a project or feature so you don't have to write it from scratch. new projects
# React (classic)
npx create-react-app my-app
# Next.js
npx create-next-app@latest my-app
# Vite
npx create-vite my-app --template react
# Express generator
npx express-generator my-apiRunning development tools without installing
# Lint your project without a global ESLint
npx eslint src/
# Format with Prettier
npx prettier --write .
# Run Prisma migrations
npx prisma migrate dev
# Generate a TypeScript config
npx tsc --initTesting a package quickly
Think of this like a try-before-you-buy. You can evaluate a utility without committing to adding it to your project:
# Spin up a quick static file server
npx http-server ./dist -p 8080
# Try lodash without installing it
npx -p lodash node -e "const _ = require('lodash'); console.log(_.chunk([1,2,3,4], 2));"Pinning versions
When you need reproducible results, like in a CI pipelineWhat is pipeline?A sequence of automated steps (install, lint, test, build, deploy) that code passes through before reaching production., pin the version explicitly:
# Always latest
npx create-react-app@latest my-app
# Specific version
npx create-react-app@4.0.0 my-appnpx command in a CI script that runs automatically. Without a version pin, a package release overnight could change your build output or break the pipeline entirely.npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. vs npm exec
npm 7 introduced npm exec as an official, semantically equivalent command. The main practical difference is argument parsing:
# These do the same thing
npx eslint src/
npm exec eslint src/
# npm exec requires -- to separate its flags from the tool's flags
npm exec -- eslint src/ --fixUse whichever feels natural. npx is shorter and more widely recognised; npm exec is what the official npm documentation now recommends.
Using npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. inside npm scripts
Because npm scripts already know about node_modules/.bin, you often do not need npx in scripts at all. But it is valid, especially for tools you want to always fetch fresh:
{
"scripts": {
"migrate": "npx prisma migrate dev",
"format": "npx prettier --write .",
"init:ts": "npx tsc --init"
}
}Package binaries
Any package that declares a bin field in its package.json can be invoked through npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version.:
{
"name": "my-cli",
"bin": {
"my-cli": "./bin/cli.js"
}
}npx my-cliThis is exactly how tools like prisma, eslint, and tsc work, they are normal npm packages that happen to ship a binaryWhat is binary?A ready-to-run file produced by the compiler. You can send it to any computer and it just works - no install needed..
Quick reference
| Task | Command |
|---|---|
| Run a tool temporarily | npx some-tool |
| Pin to a specific version | npx some-tool@1.2.3 |
| Always fetch latest | npx some-tool@latest |
| Use local version | npx tsc (reads node_modules/.bin/tsc) |
| npm 7+ equivalent | npm exec -- some-tool --flag |
| Skip confirmation prompt | npx --yes some-tool |