Skip to content
Book a call

Building Next.js on a server with 1.9 GB of memory

· 4-minute read · Engineering · by Direct Softworks

When the CI minutes ran out, the build moved onto the production box. It compiled, then died in the type check. Two settings and one habit made it finish.

Normally our site deploys the way most container setups do. A push to the main branch runs a CI workflow, the workflow builds the image, and the server pulls it and restarts. The server never compiles anything.

In September the CI minutes for the billing period ran out. A push to main still succeeded, the workflow never built anything, and the server went on serving the previous image without complaint. Until the quota reset, the only machine available to build the image was the production server itself: one virtual CPU and 1.9 GB of memory.

It can be done. It took two settings, one habit, and learning to read a build log from further up than the last line.

It compiled, then it died

The first attempt got further than expected. next build finished compiling in a little over two minutes, then failed in the TypeScript pass with "Reached heap limit".

Node's JavaScript engine, V8, sizes its default heap from the machine it finds. On this box the old-space limit worked out at roughly 950 MB. Compiling the application fits inside that. Type-checking the whole project does not. The limit that killed the build was not the machine's memory. It was V8's estimate of how much it ought to use.

The fix is one line in the build stage of the Dockerfile:

ENV NODE_OPTIONS=--max-old-space-size=2048

That is a ceiling, not a reservation. V8 does not take 2 GB at startup; it is simply allowed to grow that far before giving up. And because it sits in the build stage of a multi-stage Dockerfile, the image that actually runs the site does not inherit it. The server process keeps its default.

Swap, because 2048 is more than the box has

A 2 GB ceiling on a 1.9 GB machine only works if there is somewhere for the excess to go. We added a 3 GB swap file for the build and left it on.

Swapping during a type check is slow, and it finishes. A full build from a warm package cache takes roughly six to eight minutes. Slow and finished beats fast and killed.

One honest caveat: that swap file is not mounted at boot. After a reboot, the first build fails exactly as the first one did until someone turns it back on. That is written down in the deploy notes, which is the minimum, and not the same as fixed.

Build detached, or the build dies with your session

The build is started over SSH, and an SSH session is a fragile parent. A connection that drops takes its child processes with it. Our first instinct, nohup with an ampersand, did not survive the session closing. Running the build as a transient systemd unit with systemd-run, with its output appended to a log file, did. The build then belongs to the service manager, not to a terminal, and you can disconnect, reconnect and follow the log.

The deploy step that follows gets the same treatment, because it also outlived the timeout of the tool we were driving it from.

Two failures that look like code and are not

A font download that reads as a broken import. One build died at about 170 seconds with a "Module not found" error naming an internal font module. It looked like a bad import. Sixty lines further up the log was the real cause: a connection timeout while downloading a font file. next/font fetches Google fonts at build time, so a slow network looks like a code error. A plain retry built fine. Search the log for "timed out" before you touch the code.

Pages that deploy empty. The image is built without access to the database, so every page that regenerates on a timer ships with its empty fallback. After one deploy, the blog index said there were no published articles until somebody visited it after the revalidation window. A prerendered page counts as fresh for its window measured from build time, not from deploy time, so the first real regeneration can arrive several minutes after the new container starts. The deploy script now requests every sitemap URL twice, keeps retrying until the blog index has posts in it, and fails loudly if it never does.

Before any of this, the image that is currently serving gets an extra tag. Rolling back is re-tagging that image and restarting, which needs no build at all.

The wider point

Two of the problems on this machine came from software sizing itself to the hardware: V8 deciding on a heap, and, on the same box, Prisma computing a connection pool of three. Both defaults are reasonable. Both were decided by a formula nobody on the project chose, and both only became visible when the machine was small.

The empty pages belong to the other family, values decided at build time rather than when the server starts. In both cases the question that saves an afternoon is the same: what decided this number, and when? Ask it before you start changing code that was never wrong.

More notes