How To Fix The “JavaScript heap out of memory” Error

Those who write complicated Node.js applications can run into the “JavaScript heap out of memory” error quite often. It can also happen when you run a program downloaded from another project. Check this guide to understand this error and the solution for it.

How To Fix The “JavaScript heap out of memory” Error

How Node.js Manages Memory

Node.js sets some memory limits by default to prevent applications from using too much memory and bogging down the whole system. The actual limits depend on your system’s architecture and the version of Node.js.

How To Fix The "JavaScript heap out of memory" Error

4GB of heap memory is the limit of the latest 64-bit Node.js versions. It consists of two spaces: old and new space. The old space is where Node.js stores older objects. Its size is controlled by the –max-old-space-size flag.

When your application produces a lot of objects, they can fill up the old heap space quickly, and Node.js will crash with a “JavaScript heap out of memory” error message.

How To Fix The Problem

You can avoid the error by raising the memory limit set by Node.js with the –max-old-space-size flag. Its value (in MB) is the new memory limit.

node –max-old-space-size=8192 yourscript.js

This command allows Node.js to execute yourscript.js with up to 8GB (8192MB) memory allocated. If you want to set this flag permanently, add it as an environment variable in your system.

On Linux/macOS, run this command:

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

On Windows: go to the Control Panel and select System. In the Advanced System Settings ta, click Environment Variables and then select New.

Enter NODE_OPTIONS and –max-old-space-size=8192 in the Variable name and Variable value fields, respectively. Finally, click Enter.


The “JavaScript heap out of memory” error happens when a Node.js program consumes too much memory in the old heap space. Raise the limit with the –max-old-space-size flag, and you should avoid the issue. Visit ITtutoria to learn more about similar errors