Fix "JavaScript heap out of memory" During Build

How to fix “JavaScript heap out of memory” during Angular/Webpack builds by increasing Node/V8 memory, with safer script options.

Oct 11, 2017 · 1 min read · 166 Words · -Views -Comments · Programming
Build Tools Node.js Angular 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

While building an Angular project with webpack, I frequently hit JavaScript heap out of memory (V8 OOM). After some digging, here’s the fix.

error

Solution

The build’s memory usage exceeds V8’s default heap size. Raise the limit.

1) In your build scripts

Add node --max_old_space_size=4096. Example below.

package.json

package.json

2) Don’t patch binaries in node_modules

Some posts suggest editing node_modules/.bin/webpack. It works but is not recommended — node_modules is third‑party code. Reinstalls or upgrades will wipe your change and it harms collaboration.

If you do patch the .bin script, it looks like this:

Use the build‑script approach instead of editing binaries.

Notes

Currently, by default v8 has a memory limit of 512mb on 32-bit systems, and 1gb on 64-bit systems. The limit can be raised by setting –max-old-space-size to a maximum of ~1gb (32-bit) and ~1.7gb (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.

This explains V8’s default heap limits and why increasing --max_old_space_size fixes the issue.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover