Next.js Standalone Build & Deploy Guide
Step 1: Enable Standalone Output
Add output: 'standalone' to your next.config.js:
module.exports = {
output: 'standalone',
// ... your other config
}
Step 2: Set Up Environment Variables
Create .env.production.local with your production values for any NEXT_PUBLIC_* variables:
# .env.production.local (don't commit this!)
NEXT_PUBLIC_API_URL=https://api.yoursite.com
NEXT_PUBLIC_SITE_URL=https://yoursite.com
When building, you also need to make sure any NEXT_PUBLIC_* env variables are set to your production values - e.g. instead of localhost, use your production domain - set those in .env.production.local.
We only need to set those public variables because server-side env vars (like database URLs, API keys) are read at runtime and can be configured in Coolify. Although you can just use all your production env if easier.
Step 3: Build Locally
If you build on Mac but deploy to Linux, the sharp image library may not work. So do this:
(Mac Optional) Install Linux sharp during build
npm install --platform=linux --arch=x64 sharp
If it doesn't install the linux dependencies, try this instead:
npm install @img/sharp-linux-x64 @img/sharp-libvips-linux-x64 --force
you should see both platforms, like this:
node_modules/@img/
├── sharp-darwin-arm64 ✅ Mac
├── sharp-libvips-darwin-arm64 ✅ Mac
├── sharp-linux-x64 ✅ Linux (new!)
└── sharp-libvips-linux-x64 ✅ Linux (new!)
Then run the build:
npm run build
This creates:
.next/standalone/ – self-contained server with minimal node_modules
.next/standalone/server.js – the minimal server (replaces next start)
.next/static/ – static assets (JS, CSS chunks)
public/ – your public files (images, fonts, etc.)
Step 4: Copy Static Assets
The standalone folder doesn't include static assets by default (Next.js assumes CDN). Copy them manually:
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
Optional: Automate with a script
You can automate the above a bit by adding to package.json:
"scripts": {
"build:standalone": "next build && cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/"
}
Then just run:
npm run build:standalone