Increase JavaScript Heap size in create-react-app project

29,750

Solution 1

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

Solution 2

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

This should work

Solution 3

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

Solution 4

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

Solution 5

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

craco --max_old_space_size=4096 build
Share:
29,750
lukaswelte
Author by

lukaswelte

Love doing tik tik

Updated on July 09, 2022

Comments

  • lukaswelte
    lukaswelte almost 2 years

    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.

    • Dan Abramov
      Dan Abramov almost 7 years
      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.
  • Dhiraj Gandhi
    Dhiraj Gandhi over 5 years
    This is giving an error : basedir=$(dirname.......) line 2 of react scripts. It says missing ) after argument list. PLease help
  • Mitul Marsoniya
    Mitul Marsoniya about 5 years
    I have same issue, Our ubuntu server ram 1GB so what i need to do ?
  • dsandrade
    dsandrade about 4 years
    For smal servers you need put less memory. To me works with --max_old_space_size=256
  • vinayak shahdeo
    vinayak shahdeo almost 4 years
    what is 4096 here 4096 GB?
  • vinayak shahdeo
    vinayak shahdeo almost 4 years
    what is 4096 here 4096 GB or MB?
  • lukaswelte
    lukaswelte almost 4 years
    This is 4096 MB so 4 GB
  • PaulMest
    PaulMest over 3 years
    @vinayakshahdeo it means 4096 MB
  • Pavan Jadda
    Pavan Jadda about 3 years
    I tried this and didn't work for me even after setting 8 Gb or 12 Gb
  • S..
    S.. about 2 years
    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?
  • fabpico
    fabpico about 2 years
    Getting FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
  • fabpico
    fabpico about 2 years
    Getting FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory