Next.js "JavaScript heap out of memory" during next build in CI
next build runs in Node, and compiling a large application can exceed V8's default heap limit. Node then aborts with a fatal out-of-memory error, killing the build regardless of how much RAM the runner has.
What this error means
next build crashes with "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory" and exit code 134 or 137, often only on bigger pages or after dependency growth.
<--- Last few GCs --->
[1234:0x...] FATAL ERROR: Reached heap limit Allocation failed -
JavaScript heap out of memory
1: 0xb7... node::Abort()Common causes
The build exceeds V8's default heap size
Node caps the old-space heap; a large bundle, many pages, or heavy source maps can push allocation past the limit and abort.
The runner has too little memory for the build
A small runner (for example 2 vCPU / few GB) is killed by the kernel (exit 137) before the build finishes.
How to fix it
Raise the Node heap limit for the build
- Set NODE_OPTIONS to increase the old-space size for the build step.
- Choose a value that fits within the runner's real memory.
- Re-run next build.
env:
NODE_OPTIONS: --max-old-space-size=4096Reduce memory pressure or use a larger runner
Disable production source maps if not needed and run the build on a runner with more RAM.
// next.config.js
module.exports = { productionBrowserSourceMaps: false }How to prevent it
- Set a sensible --max-old-space-size for large builds in CI.
- Right-size the runner so the heap limit fits in real memory.
- Avoid shipping unneeded production source maps.