DEV Community

Andrei Fedotov
Andrei Fedotov

Posted on • Updated on

Fix of Angular-cli 'JavaScript heap out of memory' error while running 'ng serve'

First option:
Run node command as:
node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve
where 8048 is new memory limit in megabytes (by default it's ~1500).

Or add the command to package.json file:

"build-serve": "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve"

and run as npm run build-serve

Second option:
The quick and effective solution for Windows
Open C:\Users\userName\%AppData%\Roaming\npm
Copy/paste the following code into ng.cmd:

@IF EXIST "%~dp0\node.exe" (
    "%~dp0\node.exe" --max_old_space_size=8048 "%~dp0\node_modules\@angular\cli\bin\ng" %* 
) ELSE (
    @SETLOCAL 
    @SET PATHEXT=%PATHEXT:;.JS;=;% 
    node --max_old_space_size=8048 "%~dp0\node_modules\@angular\cli\bin\ng" %* 
)
Enter fullscreen mode Exit fullscreen mode

UPD (7 May 2020)
Third option:
Set it as an environment variable.
SET NODE_OPTIONS=--max_old_space_size=8048
Thanks to @jonyadamit for sharing

Cheers!

Top comments (15)

Collapse
 
ivanlysianok profile image
Ivan Lysianok

Thanks a lot :) First option worked for me.

Collapse
 
rifassafir profile image
SAFIR

It worked for me too. Thanks @andreisfedotov

Collapse
 
naveenux profile image
Naveen

2023 angular 15 this solution works.
Thanks a lot :)

Collapse
 
shardymbai profile image
Scott Hardy

I don't think this is correct and only a bandaid solution for local development. CircleCI and TraviisCI have memory limits for their containers. I know CircleCI is 4GB (at least in the most common tier). I also think giving 8Gbs of swap to nodeJS is not a good idea given developers will be running other memory heavy applications (IDE, Browser etc.. ) and most developers (if lucky) will have 16GBs of memory.

Collapse
 
pepas24 profile image
Sebastian Gutierrez

Just to clarify, the max_old_space_size limit the amount of memory used for the process. So in CI environements (like BitBucket, Travis CI, etc) you can use this configuration to avoid use more than that. Instead of setting 8Gbs or 12Gb set ony the maximum your environment allows 4Gb for most common CI. This solved an issue with Angular after upgrating to version 14.

Collapse
 
sampathlokuge profile image
Sampath Lokuge • Edited

This helps me a lot. Thanks a lot!

i.e. 1st option worked for me.

Collapse
 
hwaynefair profile image
Wayne Fair

You're a life-saver! Thanks!

Collapse
 
sameech profile image
Samee Ch

Welldone, I just run this command in CMD "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve" And it's work for me :-)

Collapse
 
nileshsutar4755 profile image
Nilesh Sutar

Thanks man, it helps me a lot...!

Collapse
 
jonyadamit profile image
Jonathan Adamit

Thanks!
Option #3: Set it as an environment variable.
SET NODE_OPTIONS=--max_old_space_size=8048

Collapse
 
andreisfedotov profile image
Andrei Fedotov

Thanks for sharing! I added it into the note.

Collapse
 
kannanatexcelforte profile image
kannanATexcelforte

You are the master indeed !
Thanks a lot.

Collapse
 
hardik5bavishi profile image
Hardik5Bavishi

Thanks man!

2nd option helps me a lot.

It works now.

:)

Collapse
 
pritimaurya profile image
PritiMaurya

Why this error is come ?
what is root couse of this error?

Collapse
 
andreisfedotov profile image
Andrei Fedotov

The root of this problem lies in the insufficient amount of allocated memory for the application. This can happen when, for example, an application is quite large like an enterprise application. And there is not able to specify the heap allocation size with the help of ng command. However Node can be used not only to serve the application but also to perform such things like allocating more memory to the app.