To Solve FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory Error Just increase the memory usage of the node globally – not only a single script, but you can also export environment variables, like this: export NODE_OPTIONS=–max_old_space_size=4096 Now you can run your nodejs application ...
To fix JavaScript heap out of memory error, you need to add the --max-old-space-size option when running your npm command. Alternatively, you can also set the memory limit for your entire environment using a configuration file.
If you have a look at the source: github/v8, it seems that you try to reserve a very big object.According to my experience it happens if you try to parse a huge JSON object, but when I try to parse your output with JSON and node0.11.13, it just works fine.
You don't need more --stack-size
, you need more memory: --max_new_space_size
and/or --max_old_space_size
.
The only hint I can give you beside that is trying another JSON-parser and/or try to change the input format to JSON line instead of JSON only.
$ sudo npm i -g increase-memory-limit
Run from the root location of your project:
$ increase-memory-limit
This tool will append --max-old-space-size=4096 in all node calls inside your node_modules/.bin/* files.
Node.js version >= 8 - DEPRECATION NOTICE
Since NodeJs V8.0.0, it is possible to use the option --max-old-space-size
. NODE_OPTIONS=options...
$ export NODE_OPTIONS=--max_old_space_size=4096
To solve this issue you need to run your application by increasing the memory limit by using the option --max_old_space_size
. By default the memory limit of Node.js is 512 mb.
node --max_old_space_size=2000 server.js
I found that max_new_space_size
is not an option in node 4.1.1 and max_old_space_size
alone did not solve my problem. I am adding the following to my shebang and the combination of these seems to work:
#!/usr/bin/env node --max_old_space_size=4096 --optimize_for_size --max_executable_size=4096 --stack_size=4096
[EDIT]: 4096 === 4GB of memory, if your device is low on memory you may want to choose a smaller amount.
[UPDATE]: Also discovered this error while running grunt which previously was run like so:
./node_modules/.bin/grunt
After updating the command to the following it stopped having memory errors:
node --max_old_space_size=2048 ./node_modules/.bin/grunt
Note: see the warning in the comments about how this can affect Electron applications.
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 (note: not an up-to-date-link!) are permitted, which includes "--max_old_space_size"
. Note that this article's title seems a bit misleading - it seems NODE_OPTIONS had already existed, but I'm not sure it exposed this option.
So I put in my .bashrc
:export NODE_OPTIONS=--max_old_space_size=4096
The increase-memory-limit
module has been deprecated now.
As of Node.js v8.0 shipped August 2017, we can now use the NODE_OPTIONS
env variable to set the max_old_space_size
globally.
export NODE_OPTIONS=--max_old_space_size=4096
Ref URL: https://github.com/endel/increase-memory-limit
Just a variation on the answers above.
I tried the straight up node command above without success, but the suggestion from this Angular CLI issue worked for me - you create a Node script in your package.json
file to increase the memory available to Node when you run your production build.
So if you want to increase the memory available to Node to 4gb (max-old-space-size=4096
), your Node command would be node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod
. (increase or decrease the amount of memory depending on your needs as well - 4gb worked for me, but you may need more or less). You would then add it to your package.json 'scripts' section like this:
"prod": "node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod"
It would be contained in the scripts object along with the other scripts available - e.g.:
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"prod": "node --max-old-space-size=4096./node_modules/@angular/cli/bin/ng build --prod"
}
And you run it by calling npm run prod
(you may need to run sudo npm run prod
if you're on a Mac or Linux).
Note there may be an underlying issue which is causing Node to need more memory - this doesn't address that if that's the case - but it at least gives Node the memory it needs to perform the build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With