npm run <name> — for example npm run dev to start the dev server or npm run build to produce the production site in dist/.Guide
The scripts section of package.json turns long commands into short, memorable ones. Here's how it works.
The scripts section of package.json lets you give a short name to a
command you run often. Instead of remembering a long incantation, you (and everyone on the
project) run a simple, agreed-upon name. This site defines these:
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"deploy": "astro build && wrangler pages deploy dist"
} So npm run dev is really just a nickname for astro dev. Let's run them.
npm run dev starts a local development server — a temporary web
server on your own machine (usually at localhost:4321 for Astro) that rebuilds the
page instantly as you edit. It's how you preview your work while you write it. Press
Ctrl+C in the terminal to stop it.
Start the local dev server
$ npm run dev Ctrl+`)npm run devCtrl+click the localhost link it prints to open the site in your browserCtrl+C in the terminal to stop itThe dev server keeps running and watching your files. Leave it open while you work; stop it when you're done.
npm run build produces the final, optimised website as plain files
in a dist/ folder — the version you actually publish. It runs once and exits.
Running it is also the quickest way to confirm your whole project still compiles without errors.
Build the production site
$ npm run build
$ npm run preview # then preview that build locally npm run build in the terminaldist/ folder appears with the built sitenpm run preview serves that built folder so you can check it before publishingdev = work on it (live-reloading). build = produce the final files. preview = view those final files.
You might wonder why it's npm run dev rather than typing astro dev
directly. When npm runs a script, it temporarily adds your project's
node_modules/.bin folder to the path — so the script uses the exact version
of astro installed in this project. Type astro on its own and
your computer probably won't find it at all (it isn't installed globally). Running through npm is
what makes locally-installed tools "just work".
A few script names are special: start, test, stop and
restart can be run without the word run. So npm start
and npm test work, while every other script still needs the full
npm run <name>. (This project happens to use dev rather than
start, so here you'd use npm run dev.)
Shortcut vs full form
$ npm start # only works if a 'start' script exists
$ npm test # shortcut for the 'test' script
$ npm run dev # everything else needs 'run' Can't remember a project's script names? Open package.json and read the scripts block, or run 'npm run' with no name to list them.
Sometimes you want to tweak how a script runs — say, use a different port. Put your extra
options after a lone --, and npm passes everything after it straight to the
underlying tool:
Forward an option to the tool
$ npm run dev -- --port 3001 -- is handed to astro dev, so this starts the dev server on port 3001Without the --, npm would try to interpret --port itself instead of forwarding it.
Bonus — automatic hooks: if you name a script predeploy, npm runs it automatically before deploy (and postdeploy runs after). It's a handy way to chain steps, though you won't need it on day one.
Up next: those ^4.3.0 version numbers — what they mean, and the lockfile that keeps everyone's install identical.
FAQ
npm run <name> — for example npm run dev to start the dev server or npm run build to produce the production site in dist/.node_modules/.bin to the path, so the script finds locally-installed tools like astro without a global install.start, test, stop, restart — have built-in shortcuts, so npm start works. Every other script needs the explicit npm run prefix.-- so npm forwards them to the underlying tool, e.g. npm run dev -- --port 3001.