Last updated: Mar 7, 2024
Reading time·4 min
The "JavaScript heap out of memory" error occurs when the default amount of memory allocated by Node.js is insufficient to complete the given command.
To solve the error, set the --max-old-space-size
option when running the
command, e.g. --max-old-space-size=8192
.
The following 2 error messages represent the same error.
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
The first thing you should try is to use the --max-old-space-size option to set the max memory size.
The memory size is set in megabytes.
Use the following command to increase the memory limit when starting your Node.js server.
node --max-old-space-size=8192 index.js
You basically have to add the --max-old-space-size=8192
to the command that
starts your server.
The code sample sets the memory limit to 8GB.
If you get the error when installing a package, use the following command instead.
node --max-old-space-size=8192 $(which npm) install express
Make sure to replace express
with the name of the package you're trying to
install.
If you have to issue the command often, set it in the scripts
section of your
package.json
file.
{ "scripts": { "prod": "node --max-old-space-size=8192 index.js" } }
You would now issue the command with npm run prod
.
8129 divided by 8 is 1024 (1 GB). You can set the --max-old-space-size
flag to
multiples of 1024
.
Here are some examples.
# set to 4 GB node --max-old-space-size=4096 index.js # set to 6 GB node --max-old-space-size=6144 index.js # set to 8 GB node --max-old-space-size=8129 index.js # set to 10 GB node --max-old-space-size=10177 index.js # set to 12 GB node --max-old-space-size=12225 index.js # set to 14 GB node --max-old-space-size=14273 index.js
If the heap memory consumption exceeds the limit, then V8 crashes the Node.js process and throws the error.
If you set the memory too high, the additional heap usage might cause your system to run out of memory and crash random processes.
According to
the Node.js docs,
on a machine that has 2GB of memory, consider setting --max-old-space-size
to
1.5 GB to leave some memory for other uses.
--max-old-space-size
option with an environment variableAlternatively, you can set the --max-old-space-size
option using the
NODE_OPTIONS
environment variable.
NODE_OPTIONS
environment variable on macOS or LinuxIf you are on Windows, click on the following subheading:
If you are on macOS or Linux, issue the following command.
export NODE_OPTIONS=--max-old-space-size=8192
To make the environment variable persist between sessions, add the following
line toward the end of your ~/.bashrc
, ~/.bash_profile
or ~/.zshrc
file.
export NODE_OPTIONS=--max-old-space-size=8192
If you aren't sure what type of shell you use, issue the ps -p $$
command.
ps -p $$
You can use the gedit
command to edit your profile file.
sudo gedit ~/.bashrc sudo gedit ~/.bash_profile sudo gedit ~/.zshrc
Or the nano
command if you prefer to add the line in your terminal.
sudo nano ~/.bashrc sudo nano ~/.bash_profile sudo nano ~/.zshrc
If you don't have any of the specified files, check if you have a file named
~/.profile
or create a ~/.bash_profile
file.
~/.profile
is not run if ~/.bash_profile
or ~/.bash_login
exists.Once you add the export NODE_OPTIONS
line at the end of your profile file, use
the source
command to update your shell session.
source ~/.bashrc source ~/.bash_profile source ~/.zshrc
You can also close and reopen your terminal to restart the shell session if the issue persists.
Use the echo $NODE_OPTIONS
command to verify the environment variable has been
set.
echo $NODE_OPTIONS
NODE_OPTIONS
environment variable on WindowsYou can set the NODE_OPTIONS
environment variable on Windows by issuing the
following command in CMD.
setx NODE_OPTIONS --max-old-space-size=8192
setx
command, you have to restart your CMD shell for the changes to take effect.Alternatively, you can set the environment variable in PowerShell.
$env:NODE_OPTIONS="--max-old-space-size=8192"
Try to restart your server or install the npm package after increasing your max allocated memory.
Alternatively, you can set the NODE_OPTIONS
environment variable using the
graphic user interface.
Click on View advanced system settings.
Click on Environment Variables.
NODE_OPTIONS
environment variable to --max-old-space-size=8192
(8
GB) or another value, e.g. --max-old-space-size=6144
(6 GB).Click OK 3 times to add the environment variable.
Restart your CMD and PowerShell sessions for the changes to take effect.
Try to start your development server or install your npm package after making the changes.
NODE_OPTIONS
environment variable directly in package.json
Alternatively, you can set the NODE_OPTIONS
environment variable for all
operating systems directly in your package.json
file.
Open your terminal and install the cross-env package by running the following command.
npm install --save-dev cross-env
The cross-env
package is used to set environment variables universally, in a
way that works on all operating systems.
Add the command that sets the NODE_OPTIONS
environment variable right before
the command that starts your script in package.json
.
{ "scripts": { "prod": "cross-env NODE_OPTIONS='--max-old-space-size=8192' index.js" } }
Now when you issue the npm run prod
command, the NODE_OPTIONS
environment
variable gets set right before starting your server.