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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With