Skip to content
Node.js and Python3 min read

FATAL ERROR: JavaScript heap out of memory

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

Short answer

JavaScript heap out of memory means the Node.js process used all the memory V8 allows for its heap and crashed. During builds, raise the limit with `NODE_OPTIONS=--max-old-space-size=2048` (in MB) if the machine really has that memory, or build on a bigger machine; at runtime, it usually points to a leak or to loading too much data at once.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

V8, the engine inside Node.js, keeps JavaScript objects in a memory area called the heap, and it caps that area’s size. When the heap is full and garbage collection can’t free enough, the process prints FATAL ERROR: ... JavaScript heap out of memory (sometimes Ineffective mark-compacts near heap limit) and exits. When it happens tells you the cause: during a build, or after the app has been running for a while.

Causes

  • Large front-end builds: Next.js, webpack, Vite or TypeScript type-checking on a big project, especially with source maps.
  • Loading whole files or datasets into memory: a large JSON or CSV file, or a database query that returns every row.
  • Memory leaks: caches without a size limit, arrays that only grow, event listeners added on every request.
  • Too much work at once: thousands of promises started in parallel, each holding data.
  • A small machine or account: the heap limit and the real RAM available are lower than the project needs.

How to fix it

  1. Raise the heap limit for the command that crashes (value in MB):

    NODE_OPTIONS=--max-old-space-size=2048 npm run build

    Or put it in the script itself: "build": "NODE_OPTIONS=--max-old-space-size=2048 next build". This only helps if the memory really exists; otherwise the operating system or the hosting limits kill the process instead.

  2. Build somewhere with more memory. Run the build on your computer or in CI such as GitHub Actions, and deploy only the output (.next, dist, build).

  3. Reduce what the build does: turn off production source maps, run type-checking as a separate step, and remove unused large dependencies.

  4. At runtime, process data in pieces: read files with streams (fs.createReadStream), page through database results, and limit concurrency.

  5. Find leaks. Log memory every minute and watch the trend:

    setInterval(() => {
      const { heapUsed } = process.memoryUsage();
      console.log(`heapUsed ${Math.round(heapUsed / 1048576)} MB`);
    }, 60000);

    For details, start with node --inspect app.js, open Chrome DevTools and compare heap snapshots, or use --heapsnapshot-near-heap-limit=1 to save a snapshot just before the crash.

  6. Restart cleanly after fixing, and keep an eye on the log for the next few hours.

On shared hosting, a process can also hit the account’s own memory limit before V8’s. Then it ends abruptly with Killed or the site shows 508 Resource Limit Is Reached.

Prevention

  • Build in CI and deploy artifacts instead of building on the production server.
  • Put limits on every in-memory cache.
  • Stream large files and paginate large queries from the start.

On ArabyHost

On ArabyHost AI/Developer plans, each account has its own memory limit set by the plan through CloudLinux LVE, so --max-old-space-size can’t give a process more than that. You can add NODE_OPTIONS in the Environment variables section of cPanel’s Setup Node.js App. For heavy builds, build in GitHub Actions and deploy the output with automatic deploys from GitHub; the Next.js guide shows this setup. If your app needs more memory at runtime, compare plans on pricing.

FAQ

What value should I use for --max-old-space-size?

It's in megabytes and should stay below the memory the machine or account can actually give the process, leaving room for everything else. Setting it higher than the available memory just turns the crash into the process being killed.

How do I set NODE_OPTIONS on Windows?

In PowerShell run `$env:NODE_OPTIONS="--max-old-space-size=2048"` before the command; in Command Prompt use `set NODE_OPTIONS=--max-old-space-size=2048`. The `cross-env` package lets one npm script work on every system.

My app crashes with this error after running for hours. Is the limit too low?

Probably not. Memory that grows steadily until a crash is a leak: an array, cache or listener list that never shrinks. Take heap snapshots to find it rather than raising the limit.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.