Fixing "JavaScript Heap Out of Memory" in Node.js

Learn what causes the Node.js FATAL ERROR: Ineffective mark-compacts near heap limit crash and how to fix it by raising the heap size or fixing memory leaks.

Published September 16, 2026

"JavaScript heap out of memory" happens when a Node.js process exceeds V8's default heap size limit (roughly 1.5–2 GB on 64-bit systems by default) and the garbage collector can no longer free enough memory to continue.

Common causes

  • A genuinely large workload — big JSON files, huge in-memory arrays, or large build tooling (webpack, TypeScript) — that legitimately needs more heap than the default
  • A memory leak — objects that should be garbage collected are still referenced somewhere (closures, caches without eviction, event listeners never removed)

How to fix it

  • Raise the heap limit temporarily: NODE_OPTIONS=--max-old-space-size=4096 node script.js (value in MB)
  • For npm scripts, set it in package.json: "build": "node --max-old-space-size=4096 node_modules/.bin/next build"
  • If the limit only helps temporarily and usage keeps climbing, profile with --inspect and Chrome DevTools' heap snapshot to find what's actually leaking

Example

node --max-old-space-size=4096 index.js

FAQ

Is raising --max-old-space-size always safe?

It's safe as long as the machine has enough physical RAM. Raising it just delays the crash if the real issue is an unbounded memory leak — it won't fix a leak.

More JavaScript articles