Dev, Build, and Start
Learn the three core scripts: run a dev server, build for production, and serve the build.
Next.js projects ship with three main npm scripts. You use the dev server while coding, then build and start for production.
The commands
npm run dev— start the development server with hot reloading.npm run build— compile an optimized production build.npm run start— serve the production build you just created.
The build step is where Next decides which pages are static, which are dynamic, and applies optimizations.
Example
# Develop with fast refresh
npm run dev
# Create an optimized production build
npm run build
# Serve the production build
npm run startWhen to use it
- A developer runs npm run dev during feature work to get fast refresh and detailed error overlays in the browser.
- A CI job runs npm run build to verify the production bundle compiles without type errors before merging a PR.
- An ops engineer runs npm run start after deploying a build artifact to serve the optimised production app.
More examples
Start development server
next dev enables hot module replacement and fast refresh for a tight development feedback loop.
npm run dev
# or on a custom port
npx next dev --port 4000Build for production
next build compiles TypeScript, tree-shakes, and prints per-route bundle sizes to catch regressions.
npm run build
# Sample output:
# Route (app) Size First Load JS
# ┌ ○ / 5.2 kB 89.3 kB
# └ ○ /about 3.1 kB 87.2 kBServe production build
next start serves the pre-built output and must be used in production instead of the dev server.
NODE_ENV=production npm run start
# or specify port:
npx next start --port 8080
Discussion