FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory Fix

A few days ago our Ionic Framework 3 CI build stopped working when it tried to run the following command:

ionic build --prod

The error we were getting is the following:

Running app-scripts build: --prod
[12:36:35][Step 6/9] [12:36:35]  build prod started ... 
[12:36:35][Step 6/9] [12:36:35]  clean started ... 
[12:36:35][Step 6/9] [12:36:35]  clean finished in 2 ms 
[12:36:35][Step 6/9] [12:36:35]  copy started ... 
[12:36:36][Step 6/9] [12:36:36]  deeplinks started ... 
[12:36:36][Step 6/9] [12:36:36]  deeplinks finished in 639 ms 
[12:36:36][Step 6/9] [12:36:36]  ngc started ... 
[12:37:08][Step 6/9] [12:37:08]  ngc finished in 31.51 s 
[12:37:08][Step 6/9] [12:37:08]  preprocess started ... 
[12:37:08][Step 6/9] [12:37:08]  preprocess finished in 1 ms 
[12:37:08][Step 6/9] [12:37:08]  webpack started ... 
[12:37:09][Step 6/9] [12:37:09]  copy finished in 33.89 s 
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9] FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
[12:39:45][Step 6/9] <--- Last few GCs --->
[12:39:45][Step 6/9]  1: node::Abort() [/usr/local/bin/node]
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9] [10324:0x102801e00]   188080 ms: Mark-sweep 1413.3 (1493.9) -> 1413.3 (1477.9) MB, 3984.8 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 3985 ms) last resort GC in old space requested
[12:39:45][Step 6/9]  2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]
[12:39:45][Step 6/9] [10324:0x102801e00]   192147 ms: Mark-sweep 1413.3 (1477.9) -> 1413.3 (1477.9) MB, 4066.9 / 0.0 ms  last resort GC in old space requested
[12:39:45][Step 6/9]  3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9]  4: v8::internal::Factory::NewStruct(v8::internal::InstanceType) [/usr/local/bin/node]
[12:39:45][Step 6/9] <--- JS stacktrace --->
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9] ==== JS stack trace =========================================
[12:39:45][Step 6/9]  5: v8::internal::Factory::NewTuple3(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>) [/usr/local/bin/node]
[12:39:45][Step 6/9] 
[12:39:45][Step 6/9] Security context: 0x1c8c95225ee1 <JSObject>
[12:39:45][Step 6/9]  6: v8::internal::LoadIC::UpdateCaches(v8::internal::LookupIterator*) [/usr/local/bin/node]
[12:39:45][Step 6/9]  7: v8::internal::LoadIC::Load(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>) [/usr/local/bin/node]
[12:39:45][Step 6/9]     1: typeToTypeNodeHelper(aka typeToTypeNodeHelper) [/Users/mobile/Documents/buildAgent2/work/c6414cd03cae74de/node_modules/@angular-devkit/build-optimizer/node_modules/typescript/lib/typescript.js:~25875] [pc=0x169143ccb562](this=0x1c8ca3682311 <undefined>,type=0x1c8cb0e03f71 <TypeObject map = 0x1c8c588e4181>,context=0x1c8cb768bcb9 <Object map = 0x1c8c5888d151>)
[12:39:45][Step 6/9]  8: v8::internal::Runtime_LoadIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
[12:39:45][Step 6/9]     2: mapToTypeNodes(aka mapT...
[12:39:45][Step 6/9]  9: 0x16914338463d

Funny thing is, we made almost no changes to our app or our build process. So for the last 3 days, we’ve spent hours upon hours trying to figure out why our build was failing, backing out changes one by one to see which one caused the problem.

With no luck.

We updated node, npm, ionic and other packages to try and see if that was an issue was. Then we started to peg the versions of our npm packages and found the issue firebase. About a week ago Firebase released version 4.13.1, we confirmed that our package.json spec was not tied to a specific version:

"dependencies": {
"firebase": "^4.6.1",

Unfortunately, that carrot (^) meant that npm would install any 4.x version of Firebase even if we were just trying to use 4.6.1. We removed the carrot and rebuilt without any issue. For more information on the special characters in the npm version strings visit the official documentation https://docs.npmjs.com/misc/semver.

If you’re encountering the FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory check to ensure your not using or accidentally using firebase 4.13.1 and switch to a lower version. You can force that lower version by only including the version number i.e. “4.6.1” in the string defined in your package.json file.

If you’re a First Responder, or know one, check out Resgrid which is a SaaS product utilizing Microsoft Azure, providing free logistics, management and communication tools to first responder organizations like volunteer fire departments, career fire departments, EMS, search & rescue, CERT, public safety, security and disaster relief organizations.

About: Shawn Jackson

I’ve spent the last 12 years in the world of Information Technology on both the IT and Development sides of the isle. I’m currently a Software Engineer for Paylocity. In addition to working at Paylocity, I’m also the Principal of Resgrid, a cloud services company dedicated to providing logistics and management solutions to first responder organizations, volunteer and career fire departments, EMS, ambulance services, search and rescue, public safety, HAZMAT and others. My focus is building better businesses through the use of applied, targeted and tactical software development and infrastructure implementation. My passion is solving real world business problems with technology and constant learning, in the fields of technology, business and law. I hope you enjoy reading as much as I do writing. Although I may not post as often as I would wish when I do I try and have something useful to say. Although programming is a great creative outlet it sometimes is far to technical and detail oriented to be a stress free outlet for me. So I write on this blog and sometimes a couple others. I also write fiction stories in my spare time, when I have some!