Where do I set 'NODE_OPTIONS="--max-old-space-size=2048"'
Solution 1
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 npm
scripts 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"
}
Solution 2
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>
Solution 3
In your terminal type this:
export NODE_OPTIONS="--max-old-space-size=8192"
Solution 4
@Zephyr You can set the max value in your package.json
scripts": {
"start": "node --max-old-space-size=1024 ./bin/www"
}
Also, you can even set the max size while starting you node application on through command line
node --max-old-space-size=1024 <path of your main file(For example server.js)>
Hope it helps :)
Solution 5
The tricky thing here, is that you couldn't know how much memory will have on the production server, and limit the memory with a fixed value could be not the best option.
I wrote a code, to get the total memory on a linux machine, deduct 400mb (to other server resources), and assign it as node memory limit.
FREE=$(free -m);
echo "FREE: ${FREE}"
FREE_FINAL=$(echo "$FREE" | grep -F Mem: | grep -o "[0-9]*" | grep -o -m1 "^[0-9]*")
echo "FREE_FINAL: ${FREE_FINAL}"
MEM_LIMIT=$(($FREE_FINAL-400))
echo "MEM_LIMIT: ${MEM_LIMIT}"
echo " - "
export MEMORY_LIMIT="${MEM_LIMIT:=3500}";
export NODE_OPTIONS="--max-old-space-size=${MEMORY_LIMIT}"
echo "NODE_OPTIONS: ${NODE_OPTIONS}"
You can create a build.sh or start.sh file, that will be called on package.json, to set the right limit.
If you use docker, it can be added to a entrypoint.sh file (that will run on build time, but on each docker start as well).
Zephyr
Updated on July 09, 2022Comments
-
Zephyr almost 2 years
When I run "npm start" in application I get the following error - FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Most of the solutions posted online are about increasing memory with NODE_OPTIONS="--max-old-space-size=2048". But I have no idea where to set this. Some posts talked about a .bashrc file which my project does not have. I'm also on a windows system. The "npm start" command runs "npm run build-semantic && react-scripts start" as set up in package.json.
-
Zephyr almost 5 yearsMy start script needs to have "npm run build-semantic && react-scripts start" so do I just add "--max-old-space-size=1024 ./bin/www" to it?
-
Zephyr almost 5 yearsindex.js inside client/src/
-
Gurpinder almost 5 yearsIn your package.json under scripts use the following code scripts": { "start": "node --max-old-space-size=1024 <start point of your node application>" }
-
Gurpinder almost 5 yearslet me know if you are still facing an issue
-
kernal_lora over 3 yearsIs there anyway to get the old space param dynamically in package.json? I mean template like placeholder if we place in package.json automatic evaluation, is that possible and how ?
-
d2207 almost 3 yearsdidnt work, I got
error: unknown option '--max-old-space-size=6144'
-
anevaude over 2 years@d2207 - If you're on Windows, change the word "export" to "set".
-
Molomby over 2 years
max-old-space-size
is specified in megabytes, not bits, fyi -
Steven J over 2 yearsthe first option i would NOT recommand if this is on your main windows installation. I had side effects with other apps not opening like postman or whatsapp for instance. Better to set it at project level and not windows env level github.com/postmanlabs/postman-app-support/issues/6035
-
sandiejat over 2 yearsThere are some cases where env vars could be a preferred (better) choice. Examples of such cases are - creating private base layer for containers or dedicated production build nodes. Keeping settings to the project level has its advantages obviously.
-
Dilip Kumar Yadav over 2 yearsWith second option I am getting error: <--- Last few GCs ---> <--- JS stacktrace ---> # # Fatal process OOM in insufficient memory to create an Isolate #
-
sandiejat over 2 years@DilipKumarYadav Two things to note - did you use hyphens? For quite some time I used underscores mistakenly and never worked. Also, if the project is bigger, as in my case, I had to assign 10240 (10GB) for my build to succeed. There was no way around it. We had to upgrade to Angular13 and let go of some large libraries where tree shaking wasn't possible.