Heap out of memory in angular4 while ng build --prod
Asked Answered
M

5

37

I am getting an heap out of memory error while ng build --prod ,Is there any work around. its building fine when --aot=false.

Any idea ?

enter image description here

Malissamalissia answered 18/10, 2017 at 9:51 Comment(2)
node --max_old_space_size=4096Yugoslav
Why increase the ram is a solution? Also this happen in angular 12 and 13 versionsSheathing
E
37

Try running build script in package json by the following script:

"scripts": {
   "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod"
}

Reference

Erin answered 18/10, 2017 at 9:53 Comment(0)
F
26

My team was facing the same issue and this is how we resolved.

While building the project instead of ng build --prod use this

node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod --build-optimizer

OR Just add above string in your package.json file like this and to build use just npm run prod,

    {
  "name": "Deva_Application",
  "version": "1.0.0",
    "scripts": {
    "ng": "ng",
    "start": "ng serve  --proxy-config proxy.conf.json",
    "prod": "node --max_old_space_size=64384  ./node_modules/@angular/cli/bin/ng build --prod --build-optimizer --output-hashing=none",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
...
}

In case of large project increase the --max_old_space_size to 16384 to 64384.

Facula answered 12/4, 2019 at 6:26 Comment(1)
The solution worked like a charm, but --build-optimizer didn't work for me.Vauntcourier
M
23

Here are the steps i have done to fix the issue based on above post and its worked well for me.

Step-1

Open package.json

Add this code under scripts

"build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod"

enter image description here

Step-2

Open terminal and run execute this code "npm run build-prod"

enter image description here

Hope this helps

Malissamalissia answered 2/11, 2017 at 12:18 Comment(1)
So only improvement could be to use ./node_modules/.bin/ng instead of ./node_modules/@angular/cli/bin/ng. So if the path changes anytime, you use the symlink.Quintain
Q
11
export NODE_OPTIONS=--max_old_space_size=4096
Quack answered 11/1, 2020 at 10:27 Comment(2)
using this method causes less issues during CI/CDOfilia
For Windows, in the prompt: set NODE_OPTIONS=--max_old_space_size=4096Clein
A
1

While upping the heap size can for sure resolve this issue, I also recommend checking your supported browsers, see 'https://github.com/browserslist/browserslist'

When my app stopped building with 12gb I looked into other solutions and for me I was able to limit to browsers we actually support and I reduced heap size to 8gb (I suspect I could go lower but didn't bother testing)

My config now

# Browsers that we support
# Take caution when expanding out this list, as builds will require more ram '--max_old_space_size'

last 2 Chrome versions,
last 2 FireFox versions,
last 2 Edge versions,
last 2 Safari versions
last 2 Opera versions
Astound answered 15/8, 2023 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.