How to Fix JavaScript Heap Out of Memory Error


A common problem when working on a Node.js JavaScript project is the “JavaScript heap out of memory” error. This error usually occurs when the default memory allocated by your system to Node.js is not enough to run a large project.

The error is common whether you are running your project on Windows, macOS, or a Linux distribution like Ubuntu. Fortunately, there are a few simple fixes that can help resolve the “JavaScript heap out of memory” error.


What is heap memory?

Before looking to fix the error, it is helpful to understand what heap memory is and how programs use it.

Memory allocated on the system heap is also called dynamically allocated memory. It is up to the programmer to use the available memory as he sees fit. More importantly, the heap size of a program depends on the available virtual memory allocated to it.

If you are running a relatively large project, it may require more memory than the default allocated block. This can cause your project to crash and log the “Insufficient JavaScript Heap of Memory” error.

How to Fix JavaScript Heap Out of Memory on Windows

Regardless of your IDE, the “JavaScript Insufficient Memory Heap” fix is ​​the same.

You can add an environment variable through the Control Panel to increase the memory allocated to a Node.js project.

  1. Open the The start menuto research Advanced System Settings, and select the best match.
  2. In the dialog box, click Environment variablesthen click New of one or the other System Variables Where User Variables. The former applies to all users on your computer, while the latter only affects your current account.
  3. In the Variable name enter the field NODE_OPTIONS. In the variable value enter the field –max-old-space-size=4096. This value will allocate 4 GB of virtual memory to Node.js. To set a different value, multiply the amount you need in GB by 1024 (the variable value must be in MB).
  4. Click on OKAY to save your changes, then click Apply and finally click OKAY one more time. Restart your project for the changes to take effect.

You can also set an environment variable through a Windows PowerShell terminal.

Launch a PowerShell terminal, type the command below and press Enter:


If you only want to temporarily increase the heap, run the command below in a PowerShell terminal before running your project:


Once you enter this command, you can deploy/run your project using npm run dev or your own script.

Remember to always enter the required memory size in MB. Failure to do so may cause unexpected behavior in your program. It is also essential not to allocate all your available memory as this can lead to a major system failure.

How to Fix JavaScript Heap Out of Memory Error on macOS and Linux

On macOS and Linux, the heap memory fix is ​​very similar. You need to export an environment variable that specifies the amount of virtual memory allocated to Node.js.

In your terminal, before running your project, type the following command and press Enter:

export NODE_OPTIONS=--max-old-space-size=4096

This will allocate 4 GB of virtual memory to the Node.js runtime. To set a different amount of memory, replace 4096 with the required amount in MB.

Avoiding the Heap Out of Memory JavaScript Error

You can avoid this error by making sure your program is free from memory leaks. Good memory management is crucial when writing your programs, especially in a low-level language.

Although increasing the allocated memory temporarily solves the problem, you need to find the root cause and fix it.


About Author

Comments are closed.