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