Increase JavaScript Heap size in create-react-app project
Asked Answered
D

6

29

Node features the way to increase the heap size via passing in the --max-old-space-size command line flag. In create-react-app projects everything depends on the use of react-scripts. How can I pass in this parameter in such projects and where should I best do that?

Thank you for help.

Deborahdeborath answered 18/5, 2017 at 11:21 Comment(1)
Since those scripts are just JS files in a special directory you should be able to call the Node process like you usually do, and pass node_modules/.bin/react-scripts as the argument to it. It is symlinked to the actual script.Vide
D
33

Thanks a lot to @dan-abramov as his comment is the answer! (Give him the vote up in case you come across this).

You can just put e.g. node --max_old_space_size=4096 node_modules/.bin/react-scripts start in there instead of react-scripts start

Deborahdeborath answered 18/5, 2017 at 15:48 Comment(7)
This is giving an error : basedir=$(dirname.......) line 2 of react scripts. It says missing ) after argument list. PLease helpBraswell
I have same issue, Our ubuntu server ram 1GB so what i need to do ?Enloe
For smal servers you need put less memory. To me works with --max_old_space_size=256Featherstitch
what is 4096 here 4096 GB?Berkin
This is 4096 MB so 4 GBDeborahdeborath
this doesn't work for me, it still uses over 1gb of ram (when I set it to 256) and the processor goes to 300+%. Am I missing something? Is there any other way to constrain resource usage?Gnome
Getting FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memoryMuddlehead
F
29
"build": "react-scripts --max_old_space_size=4096 build"

This should work

Footwork answered 27/3, 2019 at 13:13 Comment(6)
what is 4096 here 4096 GB or MB?Berkin
@vinayakshahdeo it means 4096 MBLager
I tried this and didn't work for me even after setting 8 Gb or 12 GbDarden
Getting FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memoryMuddlehead
@Muddlehead , I also ended up with the same FATAL ERROR: MarkCompactCollector How did you resolve it?Effluvium
@Sandeepsandy I think I fixed all heap out of memory ocurrances by upgrading NodeJs.Muddlehead
G
21

You can disable generation of source maps as described in https://create-react-app.dev/docs/advanced-configuration

Indeed as per the documentation:

When set to false, source maps are not generated for a production build. This solves out of memory (OOM) issues on some smaller machines.

Which is the case when you got an Heap Out Of Memory error

To do so, create a .env file and add GENERATE_SOURCEMAP=false

Gunk answered 11/9, 2019 at 15:44 Comment(0)
G
11

One line answer, run on terminal -> export NODE_OPTIONS=--max_old_space_size=4096

Gerfalcon answered 6/9, 2019 at 12:47 Comment(0)
M
1

If you're using craco build just add the flag in like the below

craco --max_old_space_size=4096 build
Madelle answered 9/3, 2022 at 13:56 Comment(0)
B
1

Add

ENV GENERATE_SOURCEMAP=false

in your docker file. This worked for me.

Thanks!

Bubonocele answered 15/6, 2023 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.