[javascript] FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in ionic 3

When I run ionic 3 project using "ionic serve" command than getting this error:

enter image description here

This question is related to javascript ionic-framework ionic3

The answer is


For some reasons all the answer above didn't really work for me, I did the following to fix my issue:

  1. I had to first delete the node_modules folder
  2. re-install node.js on my PC and
  3. then npm install

I guess there are plenty of ways to reach this error!

On my side, I had a loop in my package.json. Project A had a dependency on project B, that had a dependency on project A.


same issue on centos server 7, but this solved my problem:

node --max-old-space-size=X node_modules/@angular/cli/bin/ng build --prod

Where X = (2048 or 4096 or 8192 o..) is the value of memory


Pls check your node version :

_x000D_
_x000D_
   $  node -v
_x000D_
_x000D_
_x000D_

If its 10.1.1 something , then you need to update your root level node version via below commands

_x000D_
_x000D_
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash


$ source ~/.nvm/nvm.sh

$ nvm ls

$ nvm install 12.18.1
_x000D_
_x000D_
_x000D_

Once done pls restart your terminal or visual studio.

It's work 100$

For ionic user pls add the below code in your package.json for ionic user

_x000D_
_x000D_
 "ionic:build": "node --max-old-space-size=16384 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
    
_x000D_
_x000D_
_x000D_


Another non-angular answer (I was facing the same issue building a react app on AWS Amplify).

As mentioned by Emmanuel it seems that it comes from the difference in the way memory is handled by node v10 vs node v12.

I tried to increase memory with no avail. But using node v12 did it.

Check how you can add nvm use $VERSION_NODE_12 to your build settings as explained by richard

frontend:
  phases:
    preBuild:
      commands:
        - nvm use $VERSION_NODE_12
        - npm ci
    build:
      commands:
        - nvm use $VERSION_NODE_12
        - node -v
        - npm run-script build

Replace the line

"start": "ng serve -o --port 4300 --configuration=en" with

"start": "node --max_old_space_size=5096 node_modules/@angular/cli/bin/ng serve -o --port 4300 --configuration=en"

NOTE:

  1. port--4300 is not constant depends upon which port you selects.

  2. --max_old_space_size=5096 too not constant; any value 1024,2048,4096 etc


node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng build --prod --build-optimizer

adding parameter --build-optimizer resolved the issue in my case.

Update:

I am not sure why adding only --build-optimizer solves the issue but as per angular docs it should be used with aot enabled, so updated command should be like below

--build-optimizer=true --aot=true

Angular build docs


If this happening on running React application on VSCode, please check your propTypes, undefined Proptypes leads to the same issue.


#!/usr/bin/env node --max-old-space-size=4096 in the ionic-app-scripts.js dint work

Modifying

node_modules/.bin/ionic-app-scripts.cmd

By adding:

@IF EXIST "%~dp0\node.exe" ( "%~dp0\node.exe" "%~dp0..@ionic\app-scripts\bin\ionic-app-scripts.js" %* ) ELSE ( @SETLOCAL @SET PATHEXT=%PATHEXT:;.JS;=;% node --max_old_space_size=4096 "%~dp0..@ionic\app-scripts\bin\ionic-app-scripts.js" %* )

Worked fianlly


I got the same error when I execute ng build command in Visual Studio code. But I can build successfully when I execute same thing in Windows command line in following sequence.

Step 1.

set NODE_OPTIONS=--max_old_space_size=4096

Step 2.

ng build

For me, the issue was having an extra node_modules folder that I renamed to node_modules_old and running an npm install to generate a fresh node_modules. Somehow the build must have still been picking up the node_modules_old folder, so I moved node_modules_old out of the directory to fix the issue.


Just type this in the terminal:

export NODE_OPTIONS="--max-old-space-size=8192"

Sometimes simplicity is the key to success. Search for while (i <= 10000) {} without increasing i in your code ;)


In my case it was a recursion that was causing react to use up all memory.

This happened when I was refactoring my code and didn't notice this.

const SumComponent = () => {
  return (
    <>
      <SumComponent />
    </>
  ) 
}

In other node apps this might look like:

const someFunction = () => {
  ...
  someFunction(); 
  ...
}

Check your folder name. If your folder name having spaces, these kind of issues will generate. Rename without spaces. hope it will work.


For me, I had a syntax error (which didn't show up) and caused this error.


Updating from Node 12 to Node 14 solved the problem for me


node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --baseHref=/baseUrl/ --prod=true

For a non-angular general answer for those who land on this question from Google:

Every time you face this error its probably because of a memory leak or difference between how Node <= 10 and Node > 10 manage memory. Usually just increasing the memory allocated to Node will allow your program to run but may not actually solve the real problem and the memory used by the node process could still exceed the new memory you allocate. I'd advise profiling memory usage in your node process when it starts running or updating to node > 10.

I had a memory leak. Here is a great article on debugging memory leaks in node.

That said, to increase the memory, in the terminal where you run your Node process:

export NODE_OPTIONS="--max-old-space-size=8192"

where values of max-old-space-size can be: [2048, 4096, 8192, 16384] etc

[UPDATE] More examples for further clarity:

export NODE_OPTIONS="--max-old-space-size=5120" #increase to 5gb
export NODE_OPTIONS="--max-old-space-size=6144" #increase to 6gb
export NODE_OPTIONS="--max-old-space-size=7168" #increase to 7gb
export NODE_OPTIONS="--max-old-space-size=8192" #increase to 8gb

# and so on...

# formula:
export NODE_OPTIONS="--max-old-space-size=(X * 1024)" #increase to Xgb

# Note: it doesn't have to be multiples of 1024. 
# max-old-space-size can be any number of memory megabytes(MB) you have available.

Try this solution which was pointed out in an old message on the forum: https://forum.ionicframework.com/t/3-7-0-ios-build-with-prod-not-working/107061/24

Open node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js

Change the first line from:

#!/usr/bin/env node

to

#!/usr/bin/env node --max-old-space-size=4096

Try values 1024 and 2048, but for a relatively large app you may need 4096.


For me it was a problem with firebase package.

Only add "@firebase/database": "0.2.1", for your package.json, reinstall node_modules and works.


For me, I encounted this problem when run eslint and prettier fix and formating with a build directory in my React project, after removing it all things worked. I guess this is because there are too many files.


In my case, fix this problem installing NodeJs,version 12.10.0


I got the same error message when I execute following statements in Visual Studio code. But I can build successfully when I execute same thing in windows command line.

npm install -g increase-memory-limit
increase-memory-limit
set NODE_OPTIONS=--max_old_space_size=4096
ng build -c deploy --build-optimizer --aot --prod --sourceMap

I have deleted the existing node module and run the below commands to fix my issue

npm install -all
npm audit fix

For me I got this error because I lost access to the output path for the dist folder set in my angular.json file. After I reconnected to the remote path with updated credentials the error went away.


Run this command in your project folder. Use serve instead of build

node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng serve  --prod --port=4202

This issue was gone, after I've updated my all libraries like nodejs, typescript, yarn, npm, etc for my project.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to ionic-framework

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in ionic 3 Xcode couldn't find any provisioning profiles matching No provider for Http StaticInjectorError Error: Cannot find module '../lib/utils/unsupported.js' while using Ionic Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android Ionic 2: Cordova is not available. Make sure to include cordova.js or run in a device/simulator (running in emulator) Error: Node Sass does not yet support your current environment: Windows 64-bit with false Failed to find 'ANDROID_HOME' environment variable Ionic android build Error - Failed to find 'ANDROID_HOME' environment variable ionic build Android | error: No installed build tools found. Please install the Android build tools

Examples related to ionic3

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in ionic 3 Component is part of the declaration of 2 modules