Skip to content
ohsudo, Home
CI/CD

Node.js "JavaScript heap out of memory": raise the limit, or find the leak

Fix Node.js "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory": check the heap limit, set max-old-space-size, and find leaks.

Intermediate3 min readApplies to Node.js 18+ · npm · Webpack · Vite · Next.js · TypeScript · Docker · GitHub Actions

by OhSudo ·

Checked on real software: Node.js 22 · November 2026How we check

Not sure this is your cause?Find it step by step: Disk full or out of memory →

<--- Last few GCs --->
[2841:0x6a8f040] 41532 ms: Mark-Compact 2041.6 (2083.4) -> 2039.9 (2084.7) MB, …
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Older versions word it FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory. Either way, Node.js reached its own heap limit, which can be lower than what the machine has. The process aborts, with exit code 134. In a container where Node.js runs as the first process (PID 1), the code is usually 139 instead, unless the container uses an init like docker run --init.

Either the work really needs more memory, which is common for big builds (Webpack, tsc, next build), or the program keeps memory it no longer needs: a leak.

TL;DR

Terminal window
# The current limit, in MB
node -e 'console.log(require("v8").getHeapStatistics().heap_size_limit / 1048576)'
# A higher limit, for one command (in MB)
NODE_OPTIONS=--max-old-space-size=4096 npm run build

1. Check the limit

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

Node.js picks the limit from the memory it sees, and the result differs by version and machine. In a container, run this inside it, as that’s the number that matters.

2. Raise it

--max-old-space-size sets the main heap’s limit, in megabytes. Set it in NODE_OPTIONS, which also reaches Node.js processes started by npm, npx and build tools:

Terminal window
export NODE_OPTIONS=--max-old-space-size=4096
npm run build

In package.json, for one script (on Linux and macOS; on Windows, use the cross-env package):

"scripts": {
"build": "NODE_OPTIONS=--max-old-space-size=4096 vite build"
}

In GitHub Actions:

- run: npm run build
env:
NODE_OPTIONS: --max-old-space-size=4096

In a Dockerfile, ENV NODE_OPTIONS=--max-old-space-size=1536. The Dockerfile Generator and the CI/CD Pipeline Generator give you files to add it to.

Leave room below the real memory. The heap isn’t all of Node.js’s memory: buffers, native code and the rest come on top. If the limit is higher than the container or machine allows, the kernel kills the process instead, with exit code 137 and no message from Node.js. About three quarters of the container’s memory is a safe start.

3. Builds: use less memory

  • TypeScript: type checking a large project in the same process as the bundler doubles the work. Run tsc --noEmit as its own step, or turn off the bundler’s own type check.
  • Source maps for production builds take a lot of memory. Turn them off if you don’t ship them.
  • Parallel jobs: running several builds at once in one CI job, such as in a monorepo, adds up. Run fewer at a time.
  • Upgrade the bundler and Node.js: newer versions often use much less memory.

4. A server that grows: find the leak

If a long-running app fails after hours or days, raising the limit only delays the crash. Common leaks are caches with no size limit, event listeners added on every request, and arrays or maps that keep growing.

Have Node.js write heap snapshots before it dies:

Terminal window
node --heapsnapshot-near-heap-limit=2 server.js

Open the .heapsnapshot files in Chrome DevTools (Memory tab) and compare them: what grows between the two is the leak. On a running process, node --inspect lets DevTools take snapshots live.

Warning

Don’t set a huge limit “to be safe”, such as 16384 on a machine with 8 GB. Node.js then uses memory it doesn’t have, and the OOM killer ends the process, or something more important, with no clear message.

Shell, we have a problem!

Similar, but different: Exit code 137 or Killed means the kernel stopped the process for using too much memory, not Node.js. RangeError: Maximum call stack size exceeded is a stack, not a heap, problem: usually endless recursion. Array buffer allocation failed is one big allocation that failed. For Java, see Java heap space.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles