When building huge sites of up to 30k static blog posts for example, it can be really resource intensive, and not possible on smaller servers with limited CPU.
For example for my site, I've got 8gb of RAM, and it still struggles to build a blog of 6000 pages. I tried next.js's experimental config, limiting to 3 cpus, and also batching up the static build with timeouts. However, it still sometimes fails:

It often failed because the next build process uses a lot of RAM, and the next build process is pinging my backend to build the content. Because both next app and my backend are on the same VPS, there's 2 large processes.
Therefore, if I can build the Next app locally, it pretty much halves the resources required to build my site. To do this, I came across this Reddit thread with various links on building locally, and deploying the output. It turns out it's possible with 'next.js output':
This post shows how I set mine up, including some steps if you're deploying on Coolify like me. Otherwise Docker is probably similar.
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.comWhen 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 sharpIf it doesn't install the linux dependencies, try this instead:
npm install @img/sharp-linux-x64 @img/sharp-libvips-linux-x64 --forceyou 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 buildThis 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:standaloneWhat You Have Now
After build, your .next/standalone/ folder contains everything needed to run:
.next/standalone/
├── .next/
│ ├── static/ # CSS, JS chunks (after copying)
│ ├── server/ # Server-side code
│ └── ...
├── node_modules/ # Only the required dependencies (minimal!)
├── public/ # Your public assets (after copying)
├── server.js # The minimal server entry point
└── package.json
This folder is completely self-contained. You can zip it, upload it, and run it anywhere with Node.js.
Step 6: Test Locally
Before deploying, test that it works:
cd .next/standalone
PORT=3000 HOSTNAME=0.0.0.0 node server.js
Visit http://localhost:3000 to verify everything works.
Step 7: Deploy to Server
Because I'm deploying on Coolify, I just need to push the files to git, and change the build and start commands on Coolify. The build command isn't actually needed any more, and the start command will use the new server:
Push to Git
Remove .next/standalone from .gitignore (or add it explicitly)
Commit the standalone folder:
git add -f .next/standalone git commit -m "Add production build" git push
Deploy on Coolify
On Coolify, set:
Build command: echo "pre-built" (or leave empty)
Start command: node .next/standalone/server.js
Environment variables: PORT=3000, HOSTNAME=0.0.0.0
This worked perfectly for me, I followed each step including installing Sharp Linux dependencies. After changing my build and start commands on Coolify, it still takes 10 mins to deploy from my ./next/standalone folder, but it works perfectly.

Buy me a coffee



