PortfolioHire me for one like this

Sample website by Chayangkoon

CodeSprout

Lesson 6 / 6

Lesson 6

Build and deploy

8 min read

During development, npm run dev favours speed of editing over speed of loading. Before visitors see your site you make a production build: an optimised version with smaller files, pre-rendered pages, and no development helpers.

Building

Running npm run build calls next build. Node.js reads every route in app, renders the pages it can render ahead of time, bundles and compresses your JavaScript and CSS, and writes the result into a .next folder. At the end it prints a table of your routes, showing which ones are static and which are rendered on request.

Option 1: run a Node.js server

The standard way to host Next.js is to keep a Node.js server running. On a server or hosting platform you install the packages, build, and start the app. Every feature works this way: pages rendered on each request, forms that save data, logins, and Route Handlers that act as your own small API.

npm install
npm run build   # next build
npm run start   # next start, listens on port 3000

Many hosting platforms run these commands for you whenever you push your code to GitHub. You can also run next start on your own virtual server or inside a Docker container.

Option 2: static export to a CDN

If every page can be rendered at build time, as with a portfolio, a menu, or this course, you can skip the server entirely. Turn on static export, and next build writes plain HTML, CSS and JavaScript files into an out folder.

next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: "export",
};

export default nextConfig;

Those files can be uploaded to any file host. A common setup is an Amazon S3 bucket to store them, with CloudFront, a content delivery network (CDN), in front. The CDN keeps copies of your pages in data centres around the world, so the site loads quickly everywhere and costs very little to run.

Which should you choose?

  • Need logins, a database, or fresh data on every visit? Run a Node.js server.
  • Content changes only when you edit it? A static export on a CDN is fast, simple and cheap.

Whichever you pick, the code you wrote in lessons 3 to 5 stays the same. That is the real strength of Next.js: start simple, and add a server only when your site needs one. Congratulations on finishing the course!

Course complete! You've reached the end of Next.js & Node.js for beginners.