32

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?

7
  • Have you found a solution to this problem yet? Commented Mar 7, 2020 at 11:48
  • 3
    No, I couldn't find a solution to this. I just ended up increasing the RAM on my VM, and moved on with things.
    – Dan
    Commented Mar 8, 2020 at 14:56
  • I also assumed using --max-old-space-size would fix it but it doesn't. The only option I see is to go the usual CI route and build it on a separate machine and push the built files to the instance
    – apnerve
    Commented Jun 10, 2020 at 6:32
  • is it possible that you are generating source maps when building a production build? I had this situation (it needed around 1.4GB) when accidentally i enabled source maps generation
    – Mike Rodov
    Commented Jun 22, 2020 at 20:11
  • 1
    Try to use Webpack bundle analyzer to check your npm packages maybe you have installed too many and too heavy
    – Dmytro S
    Commented Nov 21, 2020 at 10:16

4 Answers 4

7

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

4

In Package.json file modify the build script as below.

"build": "react-scripts --max_old_space_size=4096 build"

This should work!

Additionally in .env file you can specify not to generate source map for your project it will help your App to load faster!

GENERATE_SOURCEMAP=false
2
  • Great idea, but you can't debug your react app if you disable source map Commented Aug 11, 2022 at 3:09
  • The GENERATE_SOURCEMAP=false helps a lot, thanks
    – FelikZ
    Commented Nov 30, 2022 at 18:55
3
  1. You could add swap space on your production server. Please refer to this
  2. What node version are you using on the production server? Try to upgrade your node version using nvm or other ways. I think it is a problem for you.
1
  • 1
    This doesn't work. I added swap space (3GiB) over 1 GiB of RAM. It still runs out of memory. My node version is v16.15.1, and that doesn't help either. Commented Jun 30, 2022 at 7:39
0

I had faced a similar issue, but in my case, it helped me when I updated the react-scripts package that comes with CRA (create-react-app).

If your current version is 3.x.x upgrade it to the latest version of 3, you can find all the available version from here.

v3.x.x - https://github.com/facebook/create-react-app/blob/master/CHANGELOG-3.x.md

v2.x.x - https://github.com/facebook/create-react-app/blob/master/CHANGELOG-2.x.md

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.