Skip to content
Latchkey

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.

next build
<--- 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

  1. Set NODE_OPTIONS to increase the old-space size for the build step.
  2. Choose a value that fits within the runner's real memory.
  3. Re-run next build.
.github/workflows/ci.yml
env:
  NODE_OPTIONS: --max-old-space-size=4096

Reduce 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
// 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.

Frequently asked questions

What causes ""JavaScript heap out of memory""?
Node caps the old-space heap; a large bundle, many pages, or heavy source maps can push allocation past the limit and abort.
How do I fix "JavaScript heap out of memory"?
Raise the Node heap limit for the build

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card