Where do I set 'NODE_OPTIONS="–max-old-space-size=2048"'

In your terminal type this:

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

If you are on Unix then type

export NODE_OPTIONS=--max-old-space-size=<size in bits>

in the terminal.

If you are on windows then run

set NODE_OPTIONS=--max-old-space-size=<size in bits>

There could be multiple ways to use this flag. I'm using 8GB as an example to allocate the amount of memory but you could select as per your project.

First option

What OP was originally looking for was a way to set an Environment Variable. The NODE_OPTIONS --max-old-space-size environment variable allows to increase Node's max heap size. Setting an environmental variable allows Node to read this value from your environment and so we don't need to pass this value as an argument every time we run Node command. This is set as a global value and can be utilized by every Node process.

The process of setting an environment variable depends on the OS. Here are two SO posts on this:

  • Linux: Setting environment variables in Linux using Bash
  • Windows: How to set Environment variables from Windows

In summary, set NODE_OPTIONS.

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

If you put this command in the terminal session, you will need to do it in every new session. To avoid that, you can put that in the shell script file and the terminal will load it automatically for you.

The .bashrc file OP mentioned exists on Linux environment and most comments refer to reload the bash as a quick way like source ~/.bashrc which loads the env vars in the current session. One can always restart the terminal to reload but the former is mostly preferred! Again, ignore this if using Windows.

Second option

Now, if setting environment variable is not a preferred option, one can always use --max-old-space-size either while running the node command. Example from Nodejs.org documentation

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

Third option

Alternatively, as the OP has already answered, we can set this per project basis but the implementation might vary depending on the project.

For npmscripts this Git comments answers it Best way to set --max-old-space-size when running npm (I reckon it's hyphens not underscore):

"scripts": 
{
    "start": "cross-env NODE_OPTIONS=--max-old-space-size=8192 webpack"
}

In Angular projects, you could define it like:

"scripts":
{
    "build-prod": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build --configuration=production"
}

Tags:

Memory

Node.Js