JavaScript heap out of memory

I am trying to create an app that displays a large amount of data. While compiling the app locally, I have experienced the “JavaScript heap out of memory” error that so many other have seen.

I develop on both Mac and Windows machines. On my Mac, I fixed this issue by adding:

export NODE_OPTIONS=--max_old_space_size=16384

to my .zshrc.

On windows I modified several files in node_modules.bin to look like this: (The files I modified all started with ng.)

if (Test-Path "$basedir/node$exe") {
  & "$basedir/node$exe" --max_old_space_size=16384 "$basedir/../@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
} else {
  & "node$exe" --max_old_space_size=16384 "$basedir/../@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
}

(Yes, 16384 is 16 GB of RAM. My app isn’t nearly that big. I set it there because I have the RAM to spare and don’t want to deal with this again.)

When I try to use AppFlow to build, predictably, I get this same error. Does anyone have a solution for this? Can I set environmental variables on the VM that is doing the build? Do I modify package.json scripts to include --max_old_space_size=16384?

I have been told in the past by tech support not to include node_modules in my git repository or it won’t build so I think the fix I used for Windows is not possible here. (Willing to be wrong.)

I have seen in some older postings that I should upgrade app-scripts. I don’t have that in my package.json. I have also seen people talk about editing ionic.cmd. I don’t have that file either. I assume that advice is for older versions of ionic?

The research I have done shows that a lot of people have this problem building locally with Ionic, but I don’t see anyone discussing the problem with AppFlow. I hope that’s because the problem is trivial to fix and my inexperience is the main issue.

Thanks for reading this. Any help would be appreciated.

My package.json file for reference:

{
“name”: “redacted”,
“version”: “0.0.1”,
“author”: “Ionic Framework”,
“homepage”: “https://ionicframework.com/”,
“scripts”: {
“ng”: “ng”,
“start”: “ng serve”,
“build”: “ng build”,
“test”: “ng test”,
“lint”: “ng lint”,
“e2e”: “ng e2e”
},
“private”: true,
“dependencies”: {
@angular/cdk”: “^9.2.4”,
@angular/common”: “~9.1.6”,
@angular/core”: “~9.1.6”,
@angular/forms”: “~9.1.6”,
@angular/material”: “^9.2.4”,
@angular/platform-browser”: “~9.1.6”,
@angular/platform-browser-dynamic”: “~9.1.6”,
@angular/router”: “~9.1.6”,
@capacitor/android”: “^2.1.0”,
@capacitor/core”: “2.1.0”,
@capacitor/ios”: “^2.1.0”,
@ionic-native/core”: “^5.0.7”,
@ionic-native/splash-screen”: “^5.0.0”,
@ionic-native/status-bar”: “^5.0.0”,
@ionic/angular”: “^5.0.0”,
“rxjs”: “~6.5.1”,
“tslib”: “^1.10.0”,
“zone.js”: “~0.10.2”
},
“devDependencies”: {
@angular-devkit/build-angular”: “~0.901.5”,
@angular/cli”: “~9.1.5”,
@angular/compiler”: “~9.1.6”,
@angular/compiler-cli”: “~9.1.6”,
@angular/language-service”: “~9.1.6”,
@capacitor/cli”: “2.1.0”,
@ionic/angular-toolkit”: “^2.1.1”,
@types/jasmine”: “~3.5.0”,
@types/jasminewd2”: “~2.0.3”,
@types/node”: “^12.11.1”,
“codelyzer”: “^5.1.2”,
“jasmine-core”: “~3.5.0”,
“jasmine-spec-reporter”: “~4.2.1”,
“karma”: “~5.0.0”,
“karma-chrome-launcher”: “~3.1.0”,
“karma-coverage-istanbul-reporter”: “~2.1.0”,
“karma-jasmine”: “~3.0.1”,
“karma-jasmine-html-reporter”: “^1.4.2”,
“protractor”: “~5.4.3”,
“ts-node”: “~8.3.0”,
“tslint”: “~6.1.0”,
“typescript”: “~3.8.3”
},
“description”: “An Ionic project”
}

Quick update:

I attempted to modify package.json in the following way:

“scripts”: {
“ng”: “ng”,
“start”: “ng serve”,
“build”: “ng build –max_old_space_size=16384”,
“test”: “ng test”,
“lint”: “ng lint”,
“e2e”: “ng e2e”
}

This fails with Unknown option: ‘–max_old_space_size’ which I can understand because it isn’t documented here, however it does work on Windows.

Still open to ideas.

Another update:

It is possible to add custom variables to a build IF you are on a “growth” plan or above. See this page for details.

I created a custom environment with a key, value pair of:

key: NODE_OPTIONS
value: --max_old_space_size=XXXX

where XXXX is the size of the RAM. I believe that is working because the errors I am getting change with the value I pass in. I started values at 1024 (1 GB) and kept doubling the number. After I get to 4GB, the build fails with:

[18:42:58]: ▸ Killed
[18:42:58]: ▸ npm ERR! code ELIFECYCLE
[18:42:58]: ▸ npm ERR! errno 137

I believe this is the VM simply running out of memory. The purpose of my app is to display a lot of data, which is stored in arrays so I don’t really have the choice of making the app smaller without losing the key functionality.

Is there a way to increase the VM memory? Am I completely off-base?

Hey there! So for app flow support, if you’re a paid user, please use the “Submit a Ticket” button under the Customer support section. The forum is just for community support.

But I will talk to the team and see if there was any issues over the weekend/holiday.

So rereading this, you could try replacing your build command with

"build": "NODE_OPTIONS=max_old_space_size=16384 ng build --prod

Since NODE_OPTIONS is a env flag, this should set it and pass it down to the node instance.