nx: JavaScript heap out of memory: --max_old_space_size not working with nx

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed
  • I’m reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

Since recently I’m experiencing node memory issues during AOT compilation. As described here on the angular-cli repo, setting the --max_old_space_size helps if I build the project like

$ node --max_old_space_size=4096 ./node_modules/.bin/ng build ...

However, if I apply the same to the affected:build script it doesn’t seem to be interpreted. Possible?

I have…

"affected:build": "node --max_old_space_size=4096 ./node_modules/.bin/nx affected:build"

and then I invoke it with

$ yarn affected:build --prod --base=origin/master --parallel

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Happens on my monorepo, which I unfortunately cannot upload somewhere.

Context

Please provide any relevant information about your setup:

  • version of Nx used: 7.5.1 and 7.6.2
  • version of Angular CLI used: 7.2.2 and 7.3.1
  • version of Angular DevKit used 0.12.0 and ~0.13.1

Other

I think the important thing is to understand whether node params in this case are passed along or whether NX forks node and doesn’t pass them along.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

There is always the possibility to set the NODE_OPTIONS="--max-old-space-size=4096" ENV variable, which should propagate to all node processes that are launched. That does have the drawback, though, of propagating to all node processes launched, whether intentionally or not.

This is how I do it and it works perfect: "build:prod": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build --configuration=production"

Make a special note on the dashes - in max-old-space-size not underscores.

On Windows it only works with:

"ng": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng",

This is also a problem for me in my CI environment. My CircleCI account has a 4gb memory limit, and my builds always fail. e2e fails only sometimes. I’m using the latest nx monorepo versions and cypress.

Passing by after fixing a similar issue: that could help someone, adding "maxWorkers": 2 in my angular.json configuration fixed the issue (looks like a mix of circleci exposing a huge amount of RAM and CPU with fork-ts-checker-webpack-plugin trying to use them all).

This is also a problem for me in my CI environment. My CircleCI account has a 4gb memory limit, and my builds always fail. e2e fails only sometimes. I’m using the latest nx monorepo versions and cypress.

@juristr

Currently, we can invoke commands (say affected:build) serially or in parallel.

When we invoke things serially, we directly invoke “ng”. And we use spawnChild to do that. When we invoke things in parallel, we use npm run all , which invokes the ng npm script.

The options I see are like this:

  • Change the serial invocation to use the ng npm script as well instead of invoking ng directly. This allows you to pass the max heap size there in the package.json.
  • The second option is to make sure that we use forkChild instead of spawnChild to run ng. This should preserve the parallel invocation.

I personally prefer the first one as it gives the developer the ability to customize what they want to customize in the same fashion for serial and parallel execution