Sometimes, it also has alternative error message like this:
An example of this error can be found when you have to build the packages you installed using
npm install with the node-gyp library.
The default Node memory limit varies from version to version, but the latest Node version 15 still has a memory limit below 2GB.
--max-old-space-size option when running your npm command.
Here’s an example of increasing the memory limit to 4GB:
node --max-old-space-size=4096 index.js
If you want to add the option when running the
npm install command, then you can pass the option from Node to npm as follows:
node --max-old-space-size=4096 `which npm` install
If you still see the heap out of memory error, then you may need to increase the heap size even more. The memory size starts from
1024 for 1GB:
--max-old-space-size=1024 # increase memory to 1GB
--max-old-space-size=2048 # increase memory to 2GB
--max-old-space-size=3072 # increase memory to 3GB
--max-old-space-size=4096 # increase memory to 4GB
--max-old-space-size=8192 # increase memory to 8GB
Alternatively, you can also set the memory limit for your entire environment using a configuration file.
Set Node memory limit using configuration file
You can set the default memory limit using your terminal client’s configuration file.
If you’re using Bash, then add the following line to your
export NODE_OPTIONS=--max_old_space_size=4096 #4GB
When you’re using ZSH, then add the line above to the
Don’t forget to check the available memory in your machine before increasing the memory limit.
Too much memory allocated for Node may cause your machine to hang.
Many modules downloaded from npm have lots of dependencies on other modules, and some may need to be compiled before they can be used.