How to deploy a Next.js app
Build and run a Next.js app on Schokr with next build and next start, enough memory for the build, and the right host and port.
Updated · 2 min read
A Next.js app on Schokr runs as a normal Node.js server: install dependencies, run next build once, then run next start bound to 0.0.0.0 and your allocated port. Give the server enough memory, because the build step is the most memory-hungry part.
1. Set up package.json scripts
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start -H 0.0.0.0 -p 25571"
}
}
Replace 25571 with the port from your server's Network section. Running the build and start together in one command works too:
npm run build && npm start
That rebuilds on every restart, which is slower but always up to date.
2. Give it enough memory
| Stage | Typical memory |
|---|---|
next build on a small to medium app | 1 to 2 GB |
next start serving traffic | 256 MB to 1 GB |
Your team pool has 6 GB on the free package, so 1.5 to 2 GB for a Next.js server is reasonable. If the build is killed or you see JavaScript heap out of memory, read memory limits and Node heap.
3. Deploy
- Create a server with the Node.js template in the console.
- Upload your project with the file manager. Leave out
node_modulesand.next. Uploading a zip and extracting it in the file manager is usually faster than many small files. - In your server's Startup settings, set the start command (or the template's equivalent) so it builds and starts, as shown above.
- Start the server and watch the console. The build prints a route summary, then
next startprints the URL it is serving on. - Open
http://<server-address>:<port>.
Option: build locally with standalone output
To avoid building on the server, enable standalone output in next.config.js:
module.exports = {
output: 'standalone',
}
Run next build on your own machine. Then upload the contents of .next/standalone, plus .next/static copied to .next/standalone/.next/static and your public folder copied to .next/standalone/public. Start it with:
node server.js
The standalone server reads HOSTNAME and PORT environment variables, so set HOSTNAME=0.0.0.0 and PORT=<your-port> in its environment. This uses far less memory and disk than a full install.
Environment variables
- Variables used in the browser must start with
NEXT_PUBLIC_and are baked in at build time. Changing them requires a rebuild. - Server-only variables can live in
.env.productionor.env. See environment variables.
Things to know
- Image optimization with
next/imageworks withnext start, but uses extra CPU and memory. - There is no built-in HTTPS. See using your own domain.
- If you only need a static export (
output: 'export'), you can serve theoutfolder as a static site with much lower resource use.