ErrorNode.js
JavaScript heap out of memory
What triggers V8's JavaScript heap out of memory crash, how to raise the heap limit for a one-off large task, and when it signals a real leak.
Last updated
ErrorNode.js
<--- Last few GCs ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb01610 node::Abort() [node]
What it means: V8, the engine Node runs on, hit its memory ceiling and can't allocate more, so the process aborts rather than continue against an exhausted heap.
Likely causes
Most probable first — with how to confirm.
- 1.The task genuinely needs more memory than V8's default heap limit allows — large builds, big JSON parses, or bundling many files at once are common triggers; the exact default varies by Node version and available system memory.
- 2.A memory leak keeps objects referenced longer than they should be — unclosed listeners, growing caches, or closures holding onto large data — a heap snapshot via `--inspect` and Chrome DevTools will show what's accumulating.
- 3.You're loading a large dataset entirely into memory at once (`readFileSync` on a huge file, building one giant array) instead of streaming it.
Fixes
Safest first; destructive ones are called out.
- →Raise the heap limit for a one-off large task: `node --max-old-space-size=4096 script.js` (value in MB) — the standard first fix, and safe since it only affects that process.
- →For npm scripts, set it via `NODE_OPTIONS=--max-old-space-size=4096 npm run build` (macOS/Linux) or `set NODE_OPTIONS=--max-old-space-size=4096 && npm run build` (Windows) so the flag reaches the actual process.
- →If it recurs at the same input size, profile for a leak rather than keep raising the limit — pushing the ceiling higher only postpones the same failure.
- →Switch to streaming APIs (`fs.createReadStream`, a streaming JSON parser) for large files instead of loading everything into memory at once.