Building angular app with source map fail with “CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory”

I’m building an Angular app with this command:

ng build --prod --configuration=prod --source-map

The versions of Angular and other packages were:

tanguy@tanguy-Aspire-E5-575G:~/Projects/Piximate/web$ ng version

Angular CLI: 6.1.2
Node: 8.10.0
OS: linux x64
Angular: 6.1.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router

Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.7.2
@angular-devkit/build-angular 0.7.2
@angular-devkit/build-optimizer 0.7.2
@angular-devkit/build-webpack 0.7.2
@angular-devkit/core 0.7.2
@angular-devkit/schematics 0.7.2
@angular/cdk 6.4.2
@angular/cli 6.1.2
@angular/material 6.4.2
@ngtools/webpack 6.1.2
@schematics/angular 0.7.2
@schematics/update 0.7.2
rxjs 6.2.2
typescript 2.9.2
webpack 4.9.2

There is a Github thread about this issue: https://github.com/angular/angular-cli/issues/5618
But it has never been resolved. However it contains some potential solutions.

What finally worked for me is the solution proposed in this comment: Increase the max size of Node.js for the command by running this:

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --configuration=staging --source-map

To make my life easier, I created an alias for this command in my package.json

 

// package.json

"scripts": {
  "ng-high-mem": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng",
  "build:prod": "npm run ng-high-mem -- build --prod --configuration=prod --source-map",
  ...
}

Note the double dash “–” before passing the arguments of the build. This is needed to ensure theses arguments are not swallowed by npm run and make sure they are passed to ng-high-mem

So I can build successfully with

npm run build:staging

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s