64

When running npm install -g ionic I get the following error:

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

Is there a way to globally increase the node.js memory limit?

gunwin
  • 4,578
  • 5
  • 37
  • 59
  • Can you tell us what OS are you using and what version node and npm is? – paolord Oct 18 '16 at 16:03
  • Windows 10, Node v6.8.1, npm v3.10.8 – gunwin Oct 18 '16 at 16:04
  • Possible duplicate of [FATAL ERROR: CALL\_AND\_RETRY\_LAST Allocation failed - process out of memory](https://stackoverflow.com/questions/26094420/fatal-error-call-and-retry-last-allocation-failed-process-out-of-memory) – Ben Creasy Feb 20 '18 at 23:53

10 Answers10

62

You can launch NPM using :

node --max-old-space-size=8000 $(which npm) install -g ionic

As described here, the default is 4000 (4Gb).

Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • 2
    You can also launch : `node --max_old_space_size=8000 $(which npm) install -g ionic` – Kévin Berthommier Jul 10 '17 at 12:24
  • 3
    The default V8 memory limit is 1.7 GB. – Joël Jan 11 '19 at 02:49
  • 2
    use the option `NODE_OPTIONS=--max_old_space_size=4096` - Documentation : https://nodejs.org/api/cli.html#cli_node_options_options – sol404 May 27 '20 at 05:50
  • 14
    Just want to point out that in my case, I got a `JavaScript heap out of memory` error because my machine didn't have enough memory. In this case, I had to *decrease* `max_old_space_size`, not increase it. – nullromo Jul 22 '20 at 21:47
  • Anyone knows the Windows equivalent for this command? I'm trying to do `npm i` but getting this "out of memory" error. – Aximili Jul 19 '21 at 10:02
22

I flagged this as a duplicate, but in case anyone happens across it, I posted the modern solution to this problem over at https://stackoverflow.com/a/48895989/4200039:

As of v8.0 shipped August 2017, the NODE_OPTIONS environment variable exposes this configuration (see NODE_OPTIONS has landed in 8.x!). Per the article, only options whitelisted in the source are permitted, which includes "--max_old_space_size".

So I put in my .bashrc: export NODE_OPTIONS=--max_old_space_size=4096

Community
  • 1
  • 1
Ben Creasy
  • 3,825
  • 4
  • 40
  • 50
  • I am trying to "npm publish" a .tgz with 650 MB size (unpacked 1.4 GB) and I get this issue. NODE_OPTIONS takes effect, but I get another error message ENOBUFS at the end. – Alexander Samoylov Aug 05 '21 at 14:57
14

Run these commands

npm install -g increase-memory-limit

Run from the root location of your project:

increase-memory-limit

Look Here For more details https://www.npmjs.com/package/increase-memory-limit

Fouzia Khan
  • 251
  • 3
  • 5
  • I tried this solution, but now "npm run dev" AND "npm install" hangs for ever! any tips to solve this? – Phil Jun 15 '22 at 16:05
  • 1
    **deprecaded** As of Node.js v8.0 shipped August 2017, you can now use the NODE_OPTIONS environment variable to set the max_old_space_size globally: https://www.npmjs.com/package/increase-memory-limit – Facundo Colombier Aug 25 '22 at 18:34
11

Try, node --max-old-space-size=<size> where size is in megabytes.

paolord
  • 389
  • 1
  • 7
4

I faced similar issue and this approach didn't work for me because I was using docker so I had to increase the memory size for docker itself to fix the issue:

Docker Screenshot

Atul
  • 1,694
  • 4
  • 21
  • 30
0

In my case, I had a recursive function which I didn't observe.Recursion can make heap out of memory.

-2

For Angular 11 / 10 in pacakge.json file make below changes

Source Link

"scripts": {
    "ng": "ng",
    "start": "node --max_old_space_size=2192 ./node_modules/@angular/cli/bin/ng serve",
    "build": "node --max_old_space_size=2192 ./node_modules/@angular/cli/bin/ng build",
   .....

},

enter image description here

Code Spy
  • 9,626
  • 4
  • 66
  • 46
-4

Solved for me, after running the command:

npm update

It will update all dependencies (be careful with broken changes). Maybe the webpack-cli dependency is the cause of this trouble.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
-4

Try this lifehack

NODE_OPTIONS="--max-old-space-size=2048" npm install -g ionic

  • 6
    The only thing this "new" answer does is to repeat what half of the other answers are already saying, including the accepted answer. – Tyler2P Aug 11 '21 at 11:21
  • Would actually argue that this is different, since its not calling node directly, but using the `NODE_OPTIONS` env variable. This allows you to prefix any command that runs node with it, without doing the `$(which )` thing – Ascherer Jun 02 '23 at 17:56
-5

I had an incorrect prefix in my .npmrc file. By moving to new company devices this path didn't match anymore with the old nodejs folder. On npm install the command freezes for a long time and throws an java out of heap exception without a suitable answer.

I've deleted it and it worked.

prefix=D:\development\nodejs
Robin Bruneel
  • 1,063
  • 8
  • 22