Error in ionic cordova build android --prod

2019-04-26 16:56发布

问题:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Error when running command: ionic cordova build android --prod

Note: that ionic build android, ionic run android and ionic serve all works fine.

I've read many topics that says once the project gets bigger, it stops working.

It was working the day before issue arrive. It stopped working as we added more files and JSON for translation of app in multi-language.

Is it a known issue? Is there any solution?

PLEASE CHECK FOR MORE DETAIL : https://youtu.be/oCN7iSt8rzg

Attached Error Image:

Ionic Info:

回答1:

Resolved by modifying my ionic.cmd file in C:\Users\AppData\Roaming\npm by adding --max_old_space_size. I set mine to 8096.

@if EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" --max_old_space_size=8096 "%~dp0\node_modules\ionic\bin\ionic" %*
) ELSE (
@SETLOCAL
@set PATHEXT=%PATHEXT:;.JS;=;%
node --max_old_space_size=8096 "%~dp0\node_modules\ionic\bin\ionic" %*
)

Good luck! Check Actual Source: https://github.com/ionic-team/ionic-cli/issues/1453



回答2:

My ionic (Ionic CLI) : 3.20.0 and @ionic/app-scripts : 3.1.8 but was still getting the error.

I fixed the error by updating the build script in package.json

  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "node --max-old-space-size=2048 ./node_modules/.bin/ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  },

or

Try the below command:

$ sudo node --max-old-space-size=4096 /usr/local/bin/ionic cordova build android --prod



回答3:

I also encountered this error and I told myself that increasing the allocated memory was a workaround as something must be going wrong under the hood.

If you are using Firebase, it turns out that it is the culprit.

I changed it from "^4.12.1" to "4.6.1" (without the caret symbol) and ran npm install to downgrade it to the said anterior version.

After that, I was able to run ionic cordova build android --prod without encountering the dreaded "heap out of memory" error.

Apparently, 4.12.1 (and perhaps others) has a memory leak, hence the encountered issue.

Hope this helps!



回答4:

Even though the issue is resolved by the OP, it's a temporary workaround. The actual issue is something to do with the string operations that is involved in the project.

As the OP has already mentioned that the project involves JSON strings of huge size, the ideal fix will be track down the memory consumption during build using tools like node-memwatch and fix the same. Setting heap size of approximately 8GB may not be possible all the time and this has to increase with the further enhancements in the project too.

Also as per this link, the latest webpack release will hopefully ease out some of these problems.

UPDATE: you can also try using latest Ionic CLI version 3.5.0 which may have some improvements