Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`react-scripts build' is using 1+ GB of RAM

Tags:

I have a relatively simple React 15 website. It only has a few distinct pages, and it's mainly just text and some pictures. However, when I use react-scripts build to build the project for production deployment, I'm finding that the node process tops out at around 1.25 GB of RAM used. This hasn't been an issue when building on my workstation, but it's a problem on my production server, where I currently only have 1 GB of RAM available. Most of the time, the server kills my build due to running out of memory.

So it seems my options are to spend more money to upgrade to 2GB of RAM on the server, or find some way to reduce the memory usage. I'd like to avoid paying more, because normally I don't use more than 200 MB of RAM to run my application, and it's just building it that uses a lot of RAM.

I've seems some people recommend adding a --max-old-space-size= flag to the build, but that doesn't seem to do anything. That is, I've tried this in my package.json:

"build": "react-scripts --max-old-space-size=512 build" 

But it still uses up 1+ GB of RAM. I kick off the build via npm run build, and adding the --max-old-space-size flag to that npm command doesn't seem to do anything.

Is there anything I can do to prevent the react-scripts build process from using so much memory?

like image 637
Dan Avatar asked Sep 02 '19 20:09

Dan


People also ask

How do you resolve the JavaScript memory heap out issue that occurs while building the React application?

The memory heap out issue occurs when the heap size is not sufficient to run the application. To resolve this issue, open the package. json file, which can be found in the root folder of React application and use --max_old_space_size=4096 as like in the below code snippet.

Why React build takes too long?

The major cause for this issue is adding too many components into a single bundle file, so the loading of that bundle file might take more time. To avoid this kind of issue, we need to structure our components in an optimized way.

What does React scripts build do?

react-scripts are simply scripts to run the build tools required to transform React JSX syntax into plain JavaScript programmatically. When you run one of the scripts, the /bin/react-scripts. js will be executed to start the process. This script will look into the arguments that you passed into the call.

How many MB does it take to create a React app?

Typically, you might create a new project using Create React App, but it can take a lot of time to install over 140 MB of dependencies.


1 Answers

The answer from similar question:

You can just put --max_old_space_size for node process instead of react-script.

e.g. node --max_old_space_size=512 node_modules/.bin/react-scripts build

like image 68
Dolf Barr Avatar answered Oct 03 '22 20:10

Dolf Barr