FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory: how to fix it
Node stopped itself because V8's JavaScript heap reached its limit. Raise the limit with NODE_OPTIONS=--max-old-space-size, always below the memory the machine or container really has, then cut what the process holds: source maps and type checking in a Vite, webpack or tsc build, or the leak in a server that grows until it crashes. Set above the container's memory, the same problem comes back as exit code 137.
By Livops team, the people who build and run LivopsUpdated
What the end of the output shows:
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
The same error has other first halves. Ineffective mark-compacts near heap limit is the current V8 giving up after collections that free too little[2], and older versions of V8 said CALL_AND_RETRY_LAST[3]:
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
This page is the general case: a Vite, webpack or tsc build, a script, or a Node server that grows until it crashes. For next build, see heap out of memory during next build, which covers Next.js’s own options. If your log ends with Killed or code 137 and no FATAL ERROR, the process was killed from outside instead: see npm error code 137.
What does the error mean?
Node runs your JavaScript in V8, and V8 keeps its objects in a heap with a ceiling. When V8 reports that the heap is out of memory, Node prints FATAL ERROR: <where V8 stopped> Allocation failed - JavaScript heap out of memory and aborts[1]. Reached heap limit means the heap was still at its limit after V8 had called the near-heap-limit callback that can raise it[2].
The ceiling is the size of V8’s old memory section, set with --max-old-space-size. As use approaches it, V8 spends more and more time collecting garbage to free memory[4], so a process near its limit slows down before it fails. Without the flag, the limit is based on the system’s available memory[5], so the same project can pass on a laptop and fail on a smaller server.
The heap is not all of the process’s memory. process.memoryUsage() reports the heap (heapTotal, heapUsed) next to rss, external and arrayBuffers[5]. A process can be bigger than its heap limit, which matters when you size the limit to a container.
First decide which case you have:
- It fails during a build (
vite build,webpack,tsc, a bundler or test runner): the build holds too much at its peak. Fix 1, then Fix 2. - A running server crashes after minutes, hours or days, with memory climbing until then: something leaks. Fix 1 buys time; Fix 3 finds the cause.
Fix 1: set the heap limit explicitly, below the real memory
Give Node a number instead of letting it guess:
NODE_OPTIONS=--max-old-space-size=3072 npm run build
Or in package.json, so every build uses it:
{
"scripts": {
"build": "NODE_OPTIONS=--max-old-space-size=3072 vite build"
}
}
NODE_OPTIONS is read by every Node process that starts with it in its environment, so it also reaches tsc, webpack and the tools your script calls. An option given on the command line overrides the same option in NODE_OPTIONS[4]. The VAR=value command form works in sh; npm runs scripts with cmd.exe on Windows[7], where it does not.
How big. Below the memory the process really has. Node’s docs suggest 1536 MiB on a machine with 2 GiB, to leave memory for other uses[4]. In a container, count from the container’s limit (docker run -m[13]), not from the host.
Why not just set it huge. If the heap limit is above what the container can give, the heap never reaches it: the container’s memory runs out first, and the kernel kills the process with SIGKILL. Node prints nothing, and the log ends with Killed or exit code 137. That is a harder failure to read than this one; see npm error code 137.
To see the limit Node is really using, run the same command with your NODE_OPTIONS and print heap_size_limit from v8.getHeapStatistics()[5]:
NODE_OPTIONS=--max-old-space-size=3072 node -e \
"console.log(require('node:v8').getHeapStatistics().heap_size_limit / 1048576)"
Fix 2: in a build, hold less
A higher limit only helps if there is memory behind it. These lower what the build needs.
Vite
build.sourcemapisfalseby default[8]. If your config sets it totrueor'hidden', try the build without it.- Run type checking as its own step before the build (
tsc --noEmit && vite build), not as a plugin inside it, so the two do not hold their memory at the same time.
Vite’s build options page has no memory setting of its own[8]; after these, the heap limit and the size of the app are what is left.
webpack
- webpack’s guide says source maps are “really expensive”[10].
devtool: falseemits none, and the docs call it a good option to start with in production[9]. - With
ts-loader,transpileOnly: trueturns off type checking in the loader, andfork-ts-checker-webpack-pluginbrings it back in a separate process[10]. Each process then has its own heap limit; both still share the machine’s memory. output.pathinfo: falsedrops the path comments that put garbage-collection pressure on projects with thousands of modules[10].
Create React App
GENERATE_SOURCEMAP=false stops source maps in the production build, and the docs name this as a fix for out-of-memory problems on smaller machines[11].
tsc
tsc --extendedDiagnosticsprints aMemory usedline, so you can measure before and after[12].--explainFilessays why each file was included[12]. A wideinclude, or a stray folder of generated code, can pull thousands of files into one compilation; the TypeScript team recommends specifying only input folders[12].skipLibCheckskips checking all.d.tsfiles; the TypeScript wiki warns it can hide misconfiguration and conflicts in them[12].- Project references split a large codebase into projects, to avoid loading too many files in a single compilation[12].
Next.js
See heap out of memory during next build: its source map and type-check options are Next.js’s own.
Fix 3: in a server, find the leak
A server that dies with this after running for a while, with memory rising until then, is keeping objects it no longer needs. A bigger limit only moves the crash later.
- Watch it grow. Log
process.memoryUsage()every minute or so[5].heapUsedthat keeps climbing under steady traffic is a leak; one that rises and falls back is just load. - Capture the heap.
--heapsnapshot-near-heap-limit=1makes Node write a heap snapshot when the heap approaches its limit[4];--heapsnapshot-signal=SIGUSR2writes one when you send the signal, andv8.writeHeapSnapshot()from code[6]. - Compare two snapshots. Take one after the app has warmed up and another after using the suspect feature for a while. In Chrome DevTools’ Memory tab, load the older one first, select the newer one, switch from Summary to Comparison, and look for large positive deltas[6].
NODE_OPTIONS="--max-old-space-size=384 --heapsnapshot-near-heap-limit=1" node server.js
A warning before doing this in production: a snapshot is built in memory and can double the heap’s size, enough to crash the app[6]. Take it from an instance that can crash without taking the service down, and in a tight container expect the snapshot itself to push the process over the limit.
What to look for in the comparison: a cache or Map that only ever grows, listeners or timers added on every request and never removed, and request data kept in a module-level array.
How to reproduce it locally
Run with the same heap limit as the server, so it fails on your machine:
NODE_OPTIONS=--max-old-space-size=1536 npm run build
To also reproduce the container’s memory, so a limit set too high shows up as 137 here too:
docker run --rm -m 2g -v "$PWD":/app -w /app node:22 \
sh -c "npm ci && NODE_OPTIONS=--max-old-space-size=1536 npm run build"
On Livops
- Builds get a heap limit that fits. For a framework build (not your own
Dockerfile), Livops runs each install and build step withNODE_OPTIONS=--max-old-space-sizeset to three quarters of the build container’s memory. The first line of the build log states that memory:Build server: <region> · <n> cores, <memory>. If you setNODE_OPTIONSin Variables, Livops leaves it alone and yours is used; keep it below that ceiling, or this error turns into exit code 137. In your ownDockerfile, put it on theRUNline yourself. - A Tip when it runs out. If the build log shows
JavaScript heap out of memory, the deploy fails and the log ends with a Tip: line: setNODE_OPTIONS=--max-old-space-size=2048in the variables, turn off source maps in production (GENERATE_SOURCEMAP=false,productionBrowserSourceMaps: false), and if it still does not fit, write to us for a bigger build. The previous version keeps running. - The running app is sized by its memory tier. 256, 512, 1024, 2048, 3072 or 4096 MiB, set with
memoryinlivops.jsonwhen the project is created and in the settings after that. The build’s heap setting does not carry over to the running app: for a Node server, addNODE_OPTIONSin Variables with a limit below the tier. New values take effect on the next deploy, or at once if you restart the service. An app that goes over its tier is stopped, and the log saysKilledor exit code 137. - See the memory over time. Through the Livops MCP server,
get_metricsreturns a service’s CPU, memory and traffic for 1 hour, 24 hours or 7 days: a line that only climbs is the leak from Fix 3.
More in the docs: The build ran out of memory, Out of memory and Variables.
Questions
What does Reached heap limit Allocation failed mean?
V8, the engine inside Node, needed more memory for JavaScript objects than its heap limit allows, garbage collection could not free enough, and Node printed this and stopped. The limit is set by --max-old-space-size; by default Node chooses it from the memory of the system.
What value should max-old-space-size be?
Below the memory the process really has, leaving room for what is not the JavaScript heap. Node's docs suggest 1536 on a machine with 2 GiB. In a container, count from the container's limit, not the host's memory.
Is CALL_AND_RETRY_LAST a different error?
No. It is the same heap out of memory from older versions of V8, which named that place in their code instead of Reached heap limit. The fixes are the same.
Why did raising the limit give me exit code 137 instead?
Because the new heap limit is above the memory the container or machine can give. The kernel's out-of-memory killer stops the process first, with SIGKILL, and Node never gets to print its own message. Lower the limit to fit, or give the process more memory.
How do I set NODE_OPTIONS on Windows?
npm runs scripts with cmd.exe on Windows, so the VAR=value command form from Linux and macOS does not work there. Set the variable in the shell first, or use a tool that sets variables across platforms.
My server crashes with this after running for hours. Will a bigger limit fix it?
Only for a while, if the memory keeps growing. That is a leak: take two heap snapshots some time apart and compare them in Chrome DevTools to see which objects keep piling up.
Does Livops set the heap limit for me?
For a framework build, yes: three quarters of the build container's memory, unless you set NODE_OPTIONS yourself. For the running app it does not; add NODE_OPTIONS in Variables, below the service's memory tier.
Sources
- Node.js source — src/node_errors.cc (raw.githubusercontent.com). Checked .
- V8 source — src/heap/heap.cc (raw.githubusercontent.com). Checked .
- V8 source — src/heap/heap.cc, version 8.4 (raw.githubusercontent.com). Checked .
- Node.js docs — Command-line API (nodejs.org). Checked .
- Node.js — Understanding and tuning memory (nodejs.org). Checked .
- Node.js — Using heap snapshots (nodejs.org). Checked .
- npm docs — npm run-script (docs.npmjs.com). Checked .
- Vite — Build options (vite.dev). Checked .
- webpack — devtool (webpack.js.org). Checked .
- webpack — Build performance (webpack.js.org). Checked .
- Create React App — Advanced configuration (create-react-app.dev). Checked .
- TypeScript wiki — Performance (github.com). Checked .
- Docker docs — Resource constraints (docs.docker.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Node appRelated
- npm error code 137 or Killed during npm install or npm run build: what it meansExit code 137 is 128 + 9: the process received SIGKILL, signal 9, which it cannot catch. During npm install or npm run build that is almost always the Linux out-of-memory killer, because the machine or the container's memory limit ran out. It is not Node's own heap limit, which prints FATAL ERROR ... JavaScript heap out of memory before it stops. Run fewer heavy steps at once, turn off production source maps, keep Node's heap below the container, or give the build more memory.
- JavaScript heap out of memory during next build: how to fix itNode's JavaScript heap reached its limit while Next.js was building, and V8 stopped the process. Raise the heap limit with NODE_OPTIONS=--max-old-space-size, staying below the memory the build really has, and cut what the build holds in memory: source maps, type checking during the build, and heavy dependencies.
- npm failed with return code 1 (npm ERR! code 1): how to fix the buildnpm failed with return code 1, or npm install exited with 1, is npm ERR! code 1 in newer npm: it only says that a script npm ran exited with code 1. The real error is printed above it: usually a compile or type error, a lockfile out of sync with package.json, or a dependency whose install script failed. Find the first error line above code 1, fix that, and the build passes.
- sh: vite: not found (also next, tsc) in npm run build: how to fix itsh: 1: vite: not found, sh: next: not found or tsc: not found during npm run build (exit code 127) means the shell found no such command in node_modules/.bin. Usually the install skipped devDependencies because NODE_ENV=production or --omit=dev, the install never ran in that folder, or the tool was only installed globally on your laptop. Install it into the project and make the build's install include dev dependencies.
- PHP Fatal error: Allowed memory size of 134217728 bytes exhausted — how to fix itPHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate N bytes) means one PHP process reached its memory_limit, here 128M, PHP's default. Raise the limit for one run with php -d memory_limit=512M, in php.ini, or for Composer with COMPOSER_MEMORY_LIMIT; then fix what filled it, usually a query that loads every row at once or a response built in memory. In Laravel, chunk, lazy and streamed responses keep memory flat.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.