Next.jsMedium
What is the output option in next.config.js (standalone, export)?
output controls what next build produces. The default emits a .next folder run with next start. output: "standalone" creates a minimal self-contained server in .next/standalone with only the needed node_modules, ideal for Docker. output: "export" writes a fully static site to out/ that any static host can serve, without server features.
Default
next build then next start on a Node.js server; all features available.
"standalone"
- Uses output file tracing to copy only the files and dependencies each route needs.
- Generates
.next/standalone/server.js; run it withnode server.js. - Copy
public/and.next/static/into the standalone folder yourself (or serve them from a CDN). - Typical Docker images shrink from 1GB+ to around 100-200MB.
- In monorepos set
outputFileTracingRootto the repo root. - Supports all server features: ISR, Cache Components, Server Functions, Proxy, image optimization.
"export"
- Static HTML, CSS and JS in
out/. - Works: Server Components rendered at build time, static Route Handlers (
GETwithout request data), client-side data fetching,next/imagewith a custom loader orunoptimized. - Not supported: Server Functions, Proxy, cookies and headers, ISR and revalidation, dynamic routes without
generateStaticParams, rewrites, redirects, headers config, default image optimization, anddynamicParams: true. - Replaced the old
next exportcommand, removed in Next.js 14.
Choosing
- Docker, Kubernetes, VMs:
standalone. - Static hosting, GitHub Pages, S3 + CloudFront, or embedding in another app:
export. - Vercel and other adapter-based platforms: default (they use the Adapters API, stable in 16.2).
Code example
tsx
1// next.config.ts2import type { NextConfig } from "next";34const nextConfig: NextConfig = {5 output: "standalone", // or "export" for a static site6};7export default nextConfig;89// Dockerfile (standalone)10// FROM node:22-alpine AS build11// WORKDIR /app12// COPY . .13// RUN npm ci && npm run build14//15// FROM node:22-alpine16// WORKDIR /app17// ENV NODE_ENV=production PORT=3000 HOSTNAME=0.0.0.018// COPY --from=build /app/.next/standalone ./19// COPY --from=build /app/.next/static ./.next/static20// COPY --from=build /app/public ./public21// EXPOSE 300022// CMD ["node", "server.js"]
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.