Code Reference

Deployment

Next.js · Reference cheat sheet

Deployment

Next.js · Reference cheat sheet


📋 Overview

Deploy Next.js with a Node server (next start), a Platform-as-a-Service (Vercel, etc.), or a static export when the app has no server-only features.

🔧 Core concepts

ModeCommand / configFits
Node servernext buildnext startSSR, Route Handlers, ISR
VercelGit integrationFull Next.js feature set
DockerMulti-stage Node imageSelf-hosted
Static exportoutput: 'export'Fully static sites only
ChecklistWhy
Set production env varsSecrets and NEXT_PUBLIC_*
NODE_ENV=productionOptimizations
Health endpointOrchestrators / load balancers
Match Node versionAvoid native module mismatches

💡 Examples

Build & start:

npm run build
npm run start

Static export (next.config.ts):

import type { NextConfig } from "next";

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

Docker (sketch):

FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

FROM node:22-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
EXPOSE 3000
CMD ["npm", "start"]

⚠️ Pitfalls

  • Static export cannot use server features (SSR cookies, many Route Handlers, ISR the same way).
  • Forgetting to set env vars on the host causes runtime crashes that never appeared locally.
  • Binding only to localhost inside containers breaks external access — listen on 0.0.0.0 when required by the host.

On this page