npx create-astro.npm installs; npx runs
npm is about installing packages. npx (which comes bundled with
npm) is about running a package's command — and it can do so even if the package isn't
installed yet, fetching it temporarily just for that one run. It's perfect for one-off tasks and
for tools you don't want cluttering your system.
The classic use: starting a new project
The best example is a project generator. You only need it once, to create the project —
installing it permanently would be wasteful. So you npx it:
Create a new project, no install needed
$ npm create astro@latest
$ # which runs, under the hood:
$ npx create-astro - Open the terminal (
Ctrl+`) in the folder where you want the project - Run the command
- npx downloads the generator, runs it once, and doesn't leave it lying around
Many 'create' commands (npm create astro, npm create vite) are friendly wrappers around npx running a one-off generator.
Running this project's own tools
When a tool is installed locally (listed in package.json), npx
is the tidy way to run it directly. It looks in your project's node_modules/.bin
first, so you get the exact version this project pins — not some other version
on your machine.
Run a locally-installed tool
$ npx astro --version # the Astro version THIS project uses
$ npx wrangler whoami # check your Cloudflare login (deploy tool) - Run these in the integrated terminal
- Because both astro and wrangler are in this project's devDependencies, npx finds and runs the local copies
This is why npx astro and a 'astro' script in package.json run the same version — both resolve to node_modules/.bin first.
npx vs npm install -g
The two are easy to confuse. The difference is permanence:
npm install -g sometool— installs the tool permanently and machine-wide. Good for something you genuinely use across many projects, but it sits there forever and can drift out of date or clash between projects.npx sometool— runs it now, fetching it on the fly if needed, and leaves nothing installed. Great for occasional tools and for always getting a current version.
Rule of thumb: reach for npx for one-off and project-local commands; only install globally (-g) the handful of tools you truly use everywhere. Fewer global installs means fewer "which version am I even running?" surprises.
A quick safety note
Because npx can download and execute code, only run packages you trust — the same
common sense you'd apply before installing anything. For well-known tools (Astro, Wrangler,
ESLint and the like) it's the standard, convenient way to run a one-off command.
Up next: keeping your installed packages current and secure with npm outdated, npm update and npm audit.