541

When I run an Ionic 3 project using the ionic serve command, then I am getting this error:

Screenshot of FATAL ERROR: ineffective mark-compacts near heap limit Allocation failed - JavaScript

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Himanshu Shekhar
  • 5,483
  • 3
  • 11
  • 8
  • check this answer: https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory/66914674#66914674 – sidverma Apr 02 '21 at 06:42

46 Answers46

629

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

Most times when you face this error it’s probably because of a memory leak, an addition/version upgrade of a library or a difference in how Node.js manages memory between versions (e.g. Node.js version <= 10 and Node.js version > 10).

Usually just increasing the memory allocated to Node.js 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.js process when it starts running or updating to Node.js > 10.

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

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

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

Or for Windows:

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

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

More examples for further clarity:

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

# and so on...

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

# 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.

See the current value of max-old-space-size (in MB)

To see the current (not exact but very close) value of max-old-space-size (in MB), run in your terminal

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'
Bakr
  • 117
  • 8
Emmanuel N K
  • 8,710
  • 1
  • 31
  • 37
  • 62
    FYI, the default is 512 MB. You don't need to jump straight to 10x that amount, you could try something between 512 and 5120 first. – Cameron Hudson Jun 22 '20 at 22:42
  • 2
    Thanks this works for all types of application having memory issues. – invinciblemuffi Aug 12 '20 at 07:10
  • 42
    Just want to point out - thats its not always indicative of a memory leak. Perhaps a library you are using is using a bit more memory than it used to. For us `next.js` began crashing our app with the fast refresh feature. – Daniel Cooke Oct 20 '20 at 12:09
  • 1
    @DanielCooke what value you used for next js? can you please share what solution you used for this issue with next js – MSD Dec 04 '20 at 20:58
  • @MSD We seem to get by with 4096 max-old-space-size with next > 9.6. For production builds and dev – Daniel Cooke Dec 09 '20 at 14:11
  • @DanielCooke thanks ... tried that not working for me – MSD Dec 11 '20 at 12:11
  • 2
    Here are the docs https://nodejs.org/api/cli.html#cli_max_old_space_size_size_in_megabytes and nothing suggests that there should be steps of `integer * 1024`. – Mikhail Vasin Jul 21 '21 at 13:40
  • 3
    @MikhailVasin See the very last comments in my answer. I know it doesn't. I just put that there for those that want the common memory increments in GB. – Emmanuel N K Jul 21 '21 at 18:30
  • Is it just window thing? In windows, I had to use "max_old_space_size" instead of "max-old-space-size" (so use an underscore instead of a dash). – newman Feb 05 '22 at 16:08
  • During export 2.5 Lakh record in Excel Sheet. I have faced same issue. After run below command export NODE_OPTIONS="--max-old-space-size=5120" my issue is Resolved. – Dhaval Mojidra Oct 19 '22 at 09:36
103

In my case, I fixed this problem by installing Node.js, version 12.10.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Germán Ayala
  • 1,175
  • 1
  • 7
  • 3
  • 1
    Same here. I've just changed the version and its works. – brunocascio Nov 20 '19 at 18:40
  • 26
    For context, Node 12 has a different heap management strategy which is based on available memory instead of using defaults. More details here: https://foundation.nodejs.org/announcements/2019/04/24/node-js-foundation-and-js-foundation-merge-to-form-openjs-foundation-2 – Derek Dowling Dec 19 '19 at 01:20
  • Nothing else worked for me, this was it. Thank you so much :) – Hussain Alaidarous Jan 07 '20 at 12:54
  • We updated to Node 12 and the error went away, easier than managing heap on Node 10 – Raffaeu May 25 '20 at 09:12
  • This fixed the issue. It should be accepted as the answer – Nitish Kumar May 27 '20 at 18:33
  • 1
    @NitishKumar I'm still receiving the error with Node 12 /shrug. The accepted should be whichever best solves the OP's use case -- though you'd've hoped they would have selected one of the answers at this point. – ruffin Jun 22 '20 at 20:35
  • this one only worked for me but in angular project. – Anup Bangale Jun 30 '20 at 11:40
  • 2
    @DerekDowling Wayback cause link broken: https://web.archive.org/web/20191103115941/https://foundation.nodejs.org/announcements/2019/04/24/node-js-foundation-and-js-foundation-merge-to-form-openjs-foundation-2 – Grim Aug 27 '20 at 08:15
  • I am using node 12.18.3 and got this error. – RyanNerd May 12 '21 at 10:09
  • I was using node 11.15.0, while making build got "Allocation failed - JavaScript heap out of memory", After updating to node 12.10.0(nvm install 12.10.0) everything works like a charm!!! – Alphonse R. Dsouza Nov 24 '21 at 10:03
87

Just type this in the terminal:

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

The error occurs when you exceed the default maximum memory allowed for Node.js. All this does is increase the maximum memory allowed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 33,328
  • 25
  • 83
  • 97
85

I had the 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    after changing the value of x.. error remains the same – Sohail Ahmad Sep 13 '19 at 14:05
  • 1
    If the error continues, keep increasing the size until there is enough memory to process which will eventually prevent the error. The only time I found I couldn't get past this error was with `ng serve` and a _massive_ .js file referenced in the `scripts` section of `angular.json` that needed to be loaded into memory. – atconway Nov 05 '19 at 22:04
  • can I set the value of X greater than 8192? I have 32GB of RAM – Diego Dec 27 '19 at 16:36
  • for me, supplying max-old-space-size on the command line did NOT work. This MAY be an interaction with nvm based node? instead inside a bash script, i used 'NODE_OPTIONS="--max-old-space-size=2048" node $NG build --prod --progress=false' which worked, as opposed to 'node --max-old-space-size=2048 $NG build --prod --progress=false' which did not. I still don not know why. – Simon H Jul 23 '20 at 10:38
  • if you run this inside docker and get KILLED error, increase the RAM allocated to docker engine as well. – Nima Ajdari Mar 17 '21 at 09:59
62

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

Step 1.

set NODE_OPTIONS=--max_old_space_size=4096

Step 2.

ng build
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
41

Try this solution which was pointed out in an old message on the forum: 3.7.0: iOS build with --prod not working

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
runnerpaul
  • 5,942
  • 8
  • 49
  • 118
39

Windows

From the control panel go to SystemAdvanced system settingsEnvironment VariablesNew (user or system)

mceclip0.png

Or this can be done in PowerShell with:

$env:NODE_OPTIONS="--max-old-space-size=8192"

You can also increase this number, if necessary. We've seen folks need to increase this up to 14 GB for some larger projects!

Linux/macOS

export NODE_OPTIONS=--max-old-space-size=8192
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mubarak Patel
  • 416
  • 4
  • 3
  • I have nodeJS 10.15.3, i tried the NODE-OPTIONS solution on Windows 10 but it didn't work. Is there any other solutions ? – VenomBerry Oct 05 '21 at 10:07
  • @VenomBerry could you run WSL? Why hurt yourself by running this stuff on standard windows without a linux subsystem. – Evert Oct 18 '21 at 18:21
  • 1
    It looks like now we have to use `max-old-space-size=8192` without leading dashes in the System Settings. With `--` I got `Fatal javascript OOM in GC during deserialization` error in Node.js. But probably it is related to the latest versions only. – it3xl Dec 15 '21 at 17:12
  • 1
    @it3xl with newer node versions you still need to use -- or it won't be read as a change. The issue you are experiencing is that (at least some) newer versions of node node actually adds 24 bytes to the number you ask for, so asking for 4096MB turns into 4294967320 bytes, or 4096.00002MB which node can't deserialize... Try 4000 – mrVandal Jan 04 '22 at 13:05
  • How can we see what that value is before we tweak it?? – klewis Nov 18 '22 at 17:59
  • This worked for me. Needed to also restart Visual Studio Code. – Martin Staufcik Apr 25 '23 at 17:29
20

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.js applications this might look like:

const someFunction = () => {
  ...
  someFunction();
  ...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
16

I got the same error message when I executed the following statements in Visual Studio Code. But I can build successfully when I execute the same thing in on the 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
heinels
  • 189
  • 1
  • 6
  • 1
    directly setting --max_old_space_size=4096 in npm script or as node_options wasnt working for me it started working after installing what you stated. THANK YOU!!!!! – Caner Mar 24 '22 at 15:01
15

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


Update
Now Node.js 16 is available, and I recommend updating to the latest available version of Node.js.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
10
  1. export NODE_OPTIONS="--max-old-space-size=6144" #it will increase to 6gb.

-------If Not Solved try this 2nd step------------- 2) Just update your node version to the latest one will solve this issue.

-------If Not Solved try this 3rd step------------- 3)Just run this command in your windows terminal. set NODE_OPTIONS=--max_old_space_size=4096

Mohamed Imran
  • 149
  • 1
  • 5
9
node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --baseHref=/baseUrl/ --prod=true
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 32
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Eric Leschinski Jun 17 '19 at 01:07
9

For some reasons all the previous answers 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. reinstall Node.js on my PC and
  3. then npm install
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samuel Olubayo
  • 135
  • 1
  • 8
9

Adding parameter --build-optimizer resolved the issue in my case:

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

I am not sure why adding only --build-optimizer solves the issue, but as per the Angular documentation it should be used with ahead-of-time (AOT) compilation enabled, so the updated command should be like below:

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

Angular build documentation

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Akshay Naik
  • 669
  • 1
  • 6
  • 22
9

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

JoshuaRDBrown
  • 343
  • 5
  • 12
7

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

Sarath Mohandas
  • 472
  • 1
  • 9
  • 25
  • 1
    This one worked for me. Setting the `NODE_OPTIONS` from the command line didn't work, I'm guessing because Visual Studio uses a new process or something. Hard coding the change into my package.json `scripts` block solved it. – user101289 Mar 30 '21 at 14:46
6

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
Deepu Reghunath
  • 8,132
  • 2
  • 38
  • 47
6

I faced the same problem on Angular. Then I wrote

"serve": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng serve"

this script to package.json scripts and for me this problem solved.

And run project this command:

npm run serve
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
Ali Alizade
  • 113
  • 1
  • 4
5

Instead of using ng build, I have executed below command in terminal to fix this issue.

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

Then do ng serve.

This is how my terminal look like

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS D:\ProjectPath\Project1> node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod
R15
  • 13,982
  • 14
  • 97
  • 173
5

Run this command:

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

To check how much you have already:

> node
> v8.getHeapStatistics()
{
  total_heap_size: 6049792,
  total_heap_size_executable: 524288,
  total_physical_size: 5477720,
  total_available_size: 1094444024,
  used_heap_size: 4141728,
  heap_size_limit: 1098907648,
  malloced_memory: 8192,
  peak_malloced_memory: 582752,
  does_zap_garbage: 0,
  number_of_native_contexts: 2,
  number_of_detached_contexts: 0
}

and then heap_size_limit: 1098907648

Alan
  • 9,167
  • 4
  • 52
  • 70
4

For me it was a problem with a Firebase package.

Only add "@firebase/database": "0.2.1" for your package.json file. Reinstall node_modules and it works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pol Fernández
  • 1,198
  • 9
  • 14
3

I deleted the existing Node.js module and ran the below commands to fix my issue:

npm install -all
npm audit fix
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

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.

badjr
  • 2,166
  • 3
  • 20
  • 31
2

Please check your Node.js version:

   node -v

If it’s 10.1.1 something, then you need to update your root level Node.js version via the below commands:

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

Once done, please restart your terminal or Visual Studio.

It's working 100$.

For Ionic users, please add the below code in your package.json

For Ionic user

 "ionic:build": "node --max-old-space-size=16384 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogesh Kumar
  • 1,016
  • 11
  • 10
  • actually this works if your building web through `app-scripts`, but if you build android or script like `ionic cordova build android` how? – Muhammed Moussa Jul 20 '20 at 16:21
2

Another non-Angular answer (I was facing the same issue building a React application on AWS Amplify).

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

I tried to increase memory with no avail. But using Node.js 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kartonnade
  • 81
  • 4
2

Just run this command:

export NODE_OPTIONS="--max-old-space-size=8192"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Alwis
  • 1,337
  • 11
  • 24
2

Happened to me after Upgrading react and react-native libraries.
Solved by:

rm -rf node_modules **/node_modules && rm -rf yarn.lock **/yarn.lock && yarn cache clean && yarn install
chenop
  • 4,743
  • 4
  • 41
  • 65
1

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.

Remi
  • 51
  • 6
1

If you are developing on Windows and running into this issue while publishing, upgrade Node.js through the official site.

The memory usage handling does increase with each newer version of Node.js, although I did not find exact numbers on what the increase is.

That was the only solution that worked for me. It took a whole weekend and more for me to solve this issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nixish
  • 21
  • 3
1

I am using the latest stable version of Node.js v-14.17. I was having the same issue with new Angular Ionic projects and tried most of the previous answers without success.

Finally after upgrading to Node.js 16.4.2 LTS, it fixed this issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishal Kumar
  • 4,419
  • 1
  • 25
  • 31
  • Azure devops - After upgrading the task of node.js version from 10 to 12, it has resolved the pipeline error. – Karan Jun 28 '22 at 07:25
0

Check your folder name. If your folder name has spaces, these kind of issues will be generated. Rename without spaces.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roney Francis
  • 356
  • 2
  • 15
0

If this happening on running a React application in Visual Studio Code, please check your propTypes, undefined Proptypes leads to the same issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kishorekumaru
  • 1,500
  • 2
  • 19
  • 33
0

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.

justTech
  • 25
  • 1
  • 5
0

#!/usr/bin/env node --max-old-space-size=4096 in the ionic-app-scripts.js file didn’t 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" %*
)

finally worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Subhamay
  • 197
  • 1
  • 5
0

For me, I encountered this problem when running ESLint and Prettier with a large build directory in my React project. After removing it, all things worked.

I guess this is because there are too many files in the build directory.

Sean
  • 1,055
  • 11
  • 10
0

This issue was gone after I've updated my all libraries like, Node.js, TypeScript, Yarn, npm, etc. for my project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oguzhan Kircali
  • 453
  • 5
  • 11
0

I have a similar issue when I run angular 'ng serve':

"FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory"

In my case I found my Angular application used lazy loading. One module was already imported to itself route module, but someone made an importing it to app module too which caused the recursive(?) loading.

This causes the out of memory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alison Niu
  • 64
  • 3
0

For a non-Angular general JavaScript answer for those who land on this question from Google and want to fix this in your Dockerfile (replace 6144 with the amount of memory in megabytes which you want to allocate).

My implementation was working in Node.js v14.1.0 being executed by Docker version 20.10.5, build 55c4c88 using DockerOperator in airflow v2.0.1).

FROM node:14.1.0

# Create app directory
WORKDIR /tmp/dockerspace/

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

CMD node --max-old-space-size=6144 app.js
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
imsheth
  • 31
  • 2
  • 18
  • 36
0

In my case the error was caused by improper use of a condition in a for ... loop. Instead of:

for (let t = startNo; t <= endNo; t++) {}

I had:

for (let t = startNo; endNo; t++) {}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eggon
  • 2,032
  • 2
  • 19
  • 38
0

I had the same problem, in my case after of install aws-sdk, not the memory space. I resolved it changing the import to require like this post:

aws-sdk in node app after deploying does not start

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

For me, it was as silly as module importing itself:

// worker.js
import { worker } from './worker'
Nati Kamusher
  • 533
  • 5
  • 9
0

In the event this is useful for someone. make sure there is not recursion in our Sass. or non exiting recursion in your recently added code. in some cases max heap exceeded is a sign of this.

Cory Lewis
  • 559
  • 5
  • 6
0

Try to uninstall node.js. then download again a fresh copy to install.

CherryBlossom
  • 393
  • 4
  • 8
0

My problem got solved by using default name in import statement for @mui/icons-material. Dont know how its related to out of memory issue, but now I am running my nextJS project smoothly after this change example, instead of import { Check } from '@mui/icons-material';

I used following format

import CheckIcon from '@mui/icons-material/Check';

JTtime
  • 11
  • 1
0

For me, running

yarn cache clean

and

rm -rf node_modules

fixed the memory issue. After this

yarn install

worked fine.

PHZ.fi-Pharazon
  • 1,479
  • 14
  • 15
-10

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

Matthis Kohli
  • 1,877
  • 1
  • 21
  • 23