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