Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

memory

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.

like image 511
Zephyr Avatar asked Jul 11 '19 05:07

Zephyr


People also ask

How do you get max old space size?

By default, the memory limit in Node. js is 512 MB. To increase this amount, you need to set the memory limit argument —-max-old-space-size . It will help avoid a memory limit issue.

How can I increase the max memory for node?

If you want to increase the max memory for Node you can use --max_old_space_size option. You should set this with NODE_OPTIONS environment variable.

How much memory can Nodejs use?

By default, Node. js (up to 11. x ) uses a maximum heap size of 700MB and 1400MB on 32-bit and 64-bit platforms, respectively.


1 Answers

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" } 
like image 96
sandiejat Avatar answered Sep 18 '22 12:09

sandiejat