Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Building and deploying

The npm run dev server is convenient for development but slow and unsuitable for visitors. Before publishing, the project is built: Next.js checks types, optimises the code and renders static pages in advance. Then the built app runs on a server or is handed to a hosting service.

Building and starting

npm run build
npm run start

npm run build calls next build and puts the result into the .next folder. At the end it prints a table of routes with the ○, ● and ƒ symbols from the static rendering lesson. npm run start launches the finished app on http://localhost:3000 — this is how the site will run for visitors.

Always run the build locally before publishing: TypeScript errors that the development server only displayed stop the build here. In Next.js 16 the linter does not run during the build — check the code separately with npm run lint. The server needs Node.js 20.9 or newer.

Vercel

Vercel is the hosting service from the creators of Next.js. The easiest way is to push the project to GitHub, then choose "Add New Project" on the Vercel site and pick the repository. Vercel runs the build itself and gives an address like blog-anna.vercel.app. Every new commit is published automatically.

Environment variables such as ADMIN_PASSWORD are set in the project settings on Vercel. Note that an SQLite file is not kept between runs on such hosting, so a cloud database such as PostgreSQL is used there.

Your own Node.js server

On an ordinary server (VPS) the app runs with the same pair of commands. To avoid copying the whole node_modules folder, enable standalone mode:

// next.config.ts
import type { NextConfig } from 'next';
 
const nextConfig: NextConfig = {
  output: 'standalone',
};
 
export default nextConfig;

After npm run build, the .next/standalone folder contains a server.js file with a minimal set of dependencies. The public and .next/static folders are copied next to it, and node server.js starts it. To restart the app after crashes it is usually kept under a process manager such as PM2 or in a Docker container, with Nginx in front of it.

Static export

If the site needs no server — no forms, login or API — it can be exported as a set of HTML files:

// next.config.ts (fragment)
const nextConfig: NextConfig = {
  output: 'export',
};

npm run build creates an out folder with ready files that can go on any static hosting. Server actions, the proxy, cookies and dynamic pages without generateStaticParams do not work in this mode.

Comparison

OptionServerWhat works
VercelNo server of your ownEverything
Node.js, output: 'standalone'Your own VPSEverything
output: 'export'Any static hostingStatic pages only

Common mistake

Running npm run dev on the server instead of a build. The site works, but slowly, with debug messages and without caching. On the server always run npm run build, then npm run start or node server.js.

Thanks for reading!

Donate to the site — we work hard for you.