Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 - JavaScript heap out of memory error

In Angular 7 application, When I execute ng serve command, it throws "JavaScript heap out of memory" error.

From different SO answers, I can understand that the issue is due to insufficient memory allocation to Node. As mentioned in their answers, by executing the below command I am able to compile/build the angular application.

node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng serve

But I need this memory allocation code written inside package.json or some configuration file so that I need not to execute the whole command each and every time. Also expecting, code logic should apply to all environments and should not create any issue during build in higher environments.

Can someone please help me to fix this. Below is my package.json code.

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.config.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
like image 997
Tech Learner Avatar asked Jul 21 '26 00:07

Tech Learner


1 Answers

First run This:

 npm install -g increase-memory-limit 

Then add this line on your package.json file:

"myCustomMemoryComm ": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng"

Like this:

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

Then your further command will be every time for running the project (serve):

npm run myCustomMemoryComm -serve

Or Direct Run this on console:

 node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng

And For Production build:

  node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --baseHref=/your-project-name/ --prod=true
like image 109
Imranmadbar Avatar answered Jul 22 '26 13:07

Imranmadbar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!