Next.js deployment packages your App Router pages, Server Components, and static assets into an optimized production build. Always run a local production build first to catch TypeScript errors and missing environment variables before releasing.
npm run build
npm run start
The next build command compiles server bundles, optimizes images, and pre-renders static pages into the .next/ directory.
Vercel maintains Next.js and supports zero-config builds, automatic preview URLs for pull requests, and global edge caching.
You can run Next.js on any Linux virtual machine running Node.js 18.18+ behind Nginx or Caddy. Use PM2 to restart the server automatically if it exits.
npm ci --omit=dev
npm run build
pm2 start npm --name "web-app" -- start
Enable output: 'standalone' in next.config.ts so Next.js traces only required runtime dependencies, shrinking Docker image sizes sharply.
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
output: 'standalone',
};
export default nextConfig;
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY .next/standalone ./
COPY .next/static ./.next/static
COPY public ./public
EXPOSE 3000
CMD ["node", "server.js"]
npm run build locally before pushing: Development mode (npm run dev) skips strict TypeScript and static prerender checks that fail in CI..next/static and public/ in Docker standalone images: Standalone output requires copying those two folders manually so CSS and public images load.next start on multiple containers without shared image/cache storage: Configure a shared Redis cache handler or CDN if you scale standalone pods horizontally.Yes, set output: 'export' in next.config.ts, though dynamic Server Actions, Middleware, and on-demand image optimization will not run without a server.
NEXT_PUBLIC_* variables are baked in at next build time, whereas server-only variables are read at runtime from the container environment.
Execute your migration command in a CI step or release hook before starting the new Next.js server process.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With