# 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.

Source: https://livops.dev/en/guides/error-javascript-heap-out-of-memory

By Livops team, the people who build and run Livops. Updated 3 December 2026.

What the end of the output shows:

```text
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]:

```text
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](https://livops.dev/en/guides/error-next-build-heap-out-of-memory), 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](https://livops.dev/en/guides/error-npm-exit-code-137-killed).

## 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:

```sh
NODE_OPTIONS=--max-old-space-size=3072 npm run build
```

Or in `package.json`, so every build uses it:

```json
{
  "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](https://livops.dev/en/guides/error-npm-exit-code-137-killed).

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]:

```sh
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.sourcemap` is `false` by default[8]. If your config sets it to `true` or `'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: false` emits none, and the docs call it a good option to start with in production[9].
- With `ts-loader`, `transpileOnly: true` turns off type checking in the loader, and `fork-ts-checker-webpack-plugin` brings it back in a separate process[10]. Each process then has its own heap limit; both still share the machine's memory.
- `output.pathinfo: false` drops 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 --extendedDiagnostics` prints a `Memory used` line, so you can measure before and after[12].
- `--explainFiles` says why each file was included[12]. A wide `include`, or a stray folder of generated code, can pull thousands of files into one compilation; the TypeScript team recommends specifying only input folders[12].
- `skipLibCheck` skips checking all `.d.ts` files; 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](https://livops.dev/en/guides/error-next-build-heap-out-of-memory): 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.

1. **Watch it grow.** Log `process.memoryUsage()` every minute or so[5]. `heapUsed` that keeps climbing under steady traffic is a leak; one that rises and falls back is just load.
2. **Capture the heap.** `--heapsnapshot-near-heap-limit=1` makes Node write a heap snapshot when the heap approaches its limit[4]; `--heapsnapshot-signal=SIGUSR2` writes one when you send the signal, and `v8.writeHeapSnapshot()` from code[6].
3. **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].

```sh
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:

```sh
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:

```sh
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 with `NODE_OPTIONS=--max-old-space-size` set 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 set `NODE_OPTIONS` in **Variables**, Livops leaves it alone and yours is used; keep it below that ceiling, or this error turns into exit code 137. In your own `Dockerfile`, put it on the `RUN` line 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: set `NODE_OPTIONS=--max-old-space-size=2048` in 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 `memory` in `livops.json` when 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, add `NODE_OPTIONS` in **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 says `Killed` or exit code 137.
- **See the memory over time.** Through the Livops MCP server, `get_metrics` returns 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](https://livops.dev/en/docs/error-build#build-memory), [Out of memory](https://livops.dev/en/docs/error-start#out-of-memory) and [Variables](https://livops.dev/en/docs/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

1. [Node.js source — src/node_errors.cc](https://raw.githubusercontent.com/nodejs/node/main/src/node_errors.cc). Checked 3 December 2026.
2. [V8 source — src/heap/heap.cc](https://raw.githubusercontent.com/v8/v8/main/src/heap/heap.cc). Checked 3 December 2026.
3. [V8 source — src/heap/heap.cc, version 8.4](https://raw.githubusercontent.com/v8/v8/8.4.371.23/src/heap/heap.cc). Checked 3 December 2026.
4. [Node.js docs — Command-line API](https://nodejs.org/api/cli.html). Checked 3 December 2026.
5. [Node.js — Understanding and tuning memory](https://nodejs.org/en/learn/diagnostics/memory/understanding-and-tuning-memory). Checked 3 December 2026.
6. [Node.js — Using heap snapshots](https://nodejs.org/en/learn/diagnostics/memory/using-heap-snapshot). Checked 3 December 2026.
7. [npm docs — npm run-script](https://docs.npmjs.com/cli/v10/commands/npm-run-script). Checked 3 December 2026.
8. [Vite — Build options](https://vite.dev/config/build-options). Checked 3 December 2026.
9. [webpack — devtool](https://webpack.js.org/configuration/devtool/). Checked 3 December 2026.
10. [webpack — Build performance](https://webpack.js.org/guides/build-performance/). Checked 3 December 2026.
11. [Create React App — Advanced configuration](https://create-react-app.dev/docs/advanced-configuration/). Checked 3 December 2026.
12. [TypeScript wiki — Performance](https://github.com/microsoft/TypeScript/wiki/Performance). Checked 3 December 2026.
13. [Docker docs — Resource constraints](https://docs.docker.com/engine/containers/resource_constraints/). Checked 3 December 2026.
