Reputation: 1317
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"
},
Upvotes: 3
Views: 15141
Reputation: 5460
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
Upvotes: 4