JavaScript Heap out of memory error [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
4 min

banner

# JavaScript Heap out of memory error [Solved]

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.

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

shell
node --max-old-space-size=8192 index.js

update max memory limit using option

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.

shell
node --max-old-space-size=8192 $(which npm) install express

increase memory limit when installing package

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.

package.json
{ "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.

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

# Setting the --max-old-space-size option with an environment variable

Alternatively, you can set the --max-old-space-size option using the NODE_OPTIONS environment variable.

# Setting the NODE_OPTIONS environment variable on macOS or Linux

If you are on Windows, click on the following subheading:

If you are on macOS or Linux, issue the following command.

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

export node options variable macos linux

To make the environment variable persist between sessions, add the following line toward the end of your ~/.bashrc, ~/.bash_profile or ~/.zshrc file.

~/.bashrc
export NODE_OPTIONS=--max-old-space-size=8192

If you aren't sure what type of shell you use, issue the ps -p $$ command.

shell
ps -p $$

find what type of shell you use

You can use the gedit command to edit your profile file.

shell
sudo gedit ~/.bashrc sudo gedit ~/.bash_profile sudo gedit ~/.zshrc

Or the nano command if you prefer to add the line in your terminal.

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

Note that ~/.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.

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

shell
echo $NODE_OPTIONS

echo node options macos linux

# Setting the NODE_OPTIONS environment variable on Windows

You can set the NODE_OPTIONS environment variable on Windows by issuing the following command in CMD.

CMD
setx NODE_OPTIONS --max-old-space-size=8192

cmd set node options environment variable

If you use the setx command, you have to restart your CMD shell for the changes to take effect.

Alternatively, you can set the environment variable in PowerShell.

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

set node options environment variable in powershell

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.

  1. Click on the Start menu and type advanced system settings.

type advanced system settings

  1. Click on View advanced system settings.

  2. Click on Environment Variables.

click environment variables

  1. In the User variables section, click on the New button.

click new user variable

  1. Set the NODE_OPTIONS environment variable to --max-old-space-size=8192 (8 GB) or another value, e.g. --max-old-space-size=6144 (6 GB).

set node options environment variable

  1. Click OK 3 times to add the environment variable.

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

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

shell
npm install --save-dev cross-env

install cross env package

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.

package.json
{ "scripts": { "prod": "cross-env NODE_OPTIONS='--max-old-space-size=8192' index.js" } }

set node env directly in package json

Now when you issue the npm run prod command, the NODE_OPTIONS environment variable gets set right before starting your server.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.