JavaScript Heap Out Of Memory Error

JavaScript Heap Out Of Memory Error

How to solve the "Heap out of memory" error in JavaScript. What does this error even mean? The memory limits of different Node.js versions. How to prevent Node.js from running out of memory

I've encountered this scary-looking error on multiple occasions when my application ran out of memory.

In this short article, I'll first give you a solution that you can use to fix this issue quickly.

After that, in case you want to know more about it, I'll explain what the causes of this error can be and how to prevent it from happening in the future.

  • How to solve the "Heap out of memory" error in JavaScript
  • What does this error even mean?
  • The memory limits of different Node.js versions
  • How to prevent Node.js from running out of memory
  • Conclusion

How to solve the "Heap out of memory" error in JavaScript

The fastest way of solving this issue is by increasing the memory limit of Node. Starting from Node.js v8, you can set the limit in MB with the --max-old-space-size flag like this:

node --max-old-space-size=4096 index.js

4096 translates to 4 GB of memory. You can set the limit to whatever you like, but make sure you don't use all the available memory, or otherwise, your system might crash.

As an alternative to this, you can also set the flag in an environment variable like this:

NODE_OPTIONS="--max-old-space-size=4096" node index.js

Change the Node.js memory limits of your environment

If you want to change the memory limits of Node.js for your entire environment, you need to set the following variable in your environment's configuration file. (.bashrc, .bash_profile, sometimes .zshrc, etc.)

Add this line to your configuration file:

~/.bashrc

export NODE_OPTIONS=--max_old_space_size=4096

"npm install" heap out of memory

If you run into this issue when installing a package with npm or yarn, you can bypass the memory limit temporarily by installing the package as follows:

node --max-old-space-size=4096 $(which npm) install -g nextawesomelib

javascript programming web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.