On almost every single Angular project after a certain size, you’ll start running into these errors :

An unhandled exception occurred: Call retries were exceeded


JavaScript heap out of memory

Both of these point to the same issue. That your build process is hitting memory limits and simply crashing. It can be frustrating to debug for a few reasons.

  • Memory limits can be set for all NodeJS processes on a machine, meaning that what will build on one developers machine may not build on another because they may have already upped/lowered their memory limits.
  • Similar to the above, certain hosted build agents (e.g. Azure Devops, Github Actions etc), may have memory limits set lower/higher than you might think, meaning that builds succeed/fail here when they wouldn’t otherwise on other machines.
  • Non production builds of your angular project (e.g. Just using ng serve) build fine, but building with the production flag consumes far more memory, and so may crash only when building in production.

Luckily there is a really easy fix that gets around all of the above. While you can leave a note in the readme saying “Hey, you need to up your memory to XYZ”, a better option is to create a npm build script that sets the memory limit for that build only, and leaves everything else on the machine as is.

For example, a recent project of mine has these sets of scripts in the package.json :

"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e",
  "build-prod": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --prod",

Notice the last script. It looks complicated but it’s actually quite simple.

  • Start the Node process.
  • Set the “max old space size” to 8000MB.
  • Find the angular CLI in your node modules folder.
  • Run an angular build of production.

Now anytime we need to build for production, we only need to run “npm run build-prod”, including on our build servers.

As to what you should set your memory limit to. You should try and set it relatively high, but not enough to consume all of your memory on your machine. By default, depending on your installed version of node, the default is between 500MB to 1GB. So even allowing up to 2 or 4GB should be enough to build most Angular projects.

Wade Developer
👋 Hey, I'm Wade
Wade is a full-stack developer that loves writing and explaining complex topics. He is an expert in Angular JS and was the owner of tutorialsforangular.com which was acquired by Upmostly in July 2022.

💬 Leave a comment

Your email address will not be published. Required fields are marked *

We will never share your email with anyone else.