Avoiding Memory Issues During Docker-Vite-React Builds

In one of my more frustrating recent evenings I had added a small feature to a react project which built and tested fine on my machine and in the CI pipeline, but refused to build on the VPS. The project would hang on the npm run build command and after 10 minutes or so (!) display a nasty stack trace. I’m using a super small VPS with only 512MB of ram but had never seen this occur. There’s also a decent sized swapfile set up so memory shouldn’t be an issue anyway.

After some research I found that while docker will use all available system memory, node limits itself to 512MB by default. Since I don’t mind using swap space for the build I can boost this up quite a bit:

# was this:
RUN npm run build

# now this:
RUN NODE_OPTIONS="--max-old-space-size=1024" npm run build

While this got me back up and running, I still need to figure out what is going wrong in the build. Some think there could be a sort of memory leak when installing node_modules, so I might look into that a bit deeper. Fortunately in this case the whole point of the build is to produce an html file with some JS, so there aren’t any runtime considerations since these get served from a separate NGINX container anyway.

I didn’t add any dependencies or a significant amount of code, so it’s really strange that this would occur all of a sudden.

Leave a comment