[Google-oriented Programming(GoP)] Fix error about JavaScript heap out of memory while running Angular CLI ng serve
How to resolve it
Remember to append max_old_space_size opton to increase JavaScript memory limit(MB). By default it’s ~1500.
Recommand limit is 1024 * n, such as 4096, 8192, etc.
There are three options to use max_old_space_size.
First way: Command Option
Put max_old_space_size option to package.json script. Replace ng serve script
1 | "scripts": { |
with
1 | "scripts": { |
then run as npm start.
1 | npm start |
or run command as:
1 | node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng serve |
Second option: Environment Variable
Set it as an environment variable NODE_OPTIONS.
1 | export NODE_OPTIONS=--max_old_space_size=4096 |
then run npm or node command as normal.
Third option: npm executable file
Modify npm executable file and append --max_old_space_size=4096 option.
Find where the npm executable file.
1 | which npm |
vi ~/.nvm/versions/node/v8.9.0/bin/npm.
1 | !/usr/bin/env node --max_old_space_size=4096 |
then run npm command as normal.