admin管理员组

文章数量:1398840

In Angular 7 application, When I execute ng serve mand, 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 mand I am able to pile/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 mand 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"
  },

In Angular 7 application, When I execute ng serve mand, 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 mand I am able to pile/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 mand 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"
  },
Share Improve this question asked Apr 24, 2020 at 1:40 Tech LearnerTech Learner 1,3177 gold badges30 silver badges71 bronze badges 1
  • You can add NODE_OPTIONS ( URL - > support.snyk.io/hc/en-us/articles/… ) environment variable but it will crash your VS application if it is lower than 2017 version. – Tech Learner Commented Apr 24, 2020 at 1:44
Add a ment  | 

1 Answer 1

Reset to default 4

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 mand 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

本文标签: nodejsAngular 7JavaScript heap out of memory errorStack Overflow