Using npx
npx runs a package's executable without installing it permanently. It is how you scaffold a project (npx create-next-app), run a one-off tool, or execute a locally-installed binary by name. Bundled with npm since 5.2, it closed a real gap: previously, running a CLI meant either a global install or digging into node_modules/.bin by hand.
What npx does, in order
When you run npx <command>, it resolves the command through a clear priority chain:
npx resolution order
npx some-tool
1. Is "some-tool" in ./node_modules/.bin? → run the local copy
2. Is it already in the npx cache? → run that
3. Otherwise → download it to a temp cache, run it, keep it cached
(nothing is added to your package.json)The three everyday uses
Use case | Example |
|---|---|
Scaffold a new project |
|
Run a local tool ad-hoc |
|
Try a tool once, no install |
|
npx create-next-app@latest my-app # scaffolder — run once, never kept around npx tsc --noEmit # run the project's local TypeScript npx prettier --write . # format without a global install npx http-server # spin up a quick static server
Why “run once” tools shine here
Scaffolders like create-next-app are used exactly once per project. Installing them globally means a stale version sits on your machine forever. npx ...@latest fetches the current version each time, runs it, and never pollutes your global space:
Useful flags
Flag | Effect |
|---|---|
| Skip the "install this package?" confirmation prompt |
| Fail instead of downloading if not already present |
| Install one package but run a differently-named binary from it |
| Run a command string inside the npx-augmented PATH |
| Pin the exact package version to execute |
The -p form matters when the package name and the command it provides differ:
npx -p typescript tsc --version # package "typescript", binary "tsc"
npx vs a global install
|
| |
|---|---|---|
Version freshness | Latest (or pinned) each run | Frozen until you update it |
Disk footprint | Temp cache, prunable | Permanent global install |
Reproducibility | High with | Drifts across machines |
Best for | One-offs, scaffolds, local bins | Daily-driver CLIs you run constantly |