[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
2
3
"scripts": {
"start": "ng serve"
}

with

1
2
3
"scripts": {
"start": "node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng serve"
}

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
2
3
4
5
export NODE_OPTIONS=--max_old_space_size=4096

# check environment variable.
echo $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
2
which npm
# May be ~/.nvm/versions/node/v8.9.0/bin/npm, if use nvm.

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.

Reference

[1] Fix of Angular-cli ‘JavaScript heap out of memory’ error while running ‘ng serve’ - DEV https://dev.to/koscheyscrag/fix-of-angular-cli-javascript-heap-out-of-memory-error-while-running-ng-serve–1jjh