devCreate React App: request failed reason getaddrinfo ENOTFOUND

Ever run into this issue while trying to run create-react-app?

Well, happily there is a very simple fix that doesn't involve changing your ~/.bash_profile or ~/.bashrc files and messing with your system's ENV variables. All you do is add this to your package.json script and it should run with the local change to the environment, only for this script.

Open package.json and look for the "scripts" section. You should see something like this:

"scripts":{
    "start":"react-scripts start",
    "build":"react-scripts build",
        //...etc...
}

Just add this to the start or dev script (whatever you type to run it):

"scripts":{
    "start":"export HOST ='localhost' && react-scripts start",
    "build":"react-scripts build",
        //...etc...
}

This updates your HOST variable (the problem in this case) by exporting it right before you run the script, and only for this app, it won't affect any other variables on your system or require you to alter other setups you have for the HOST variable.

The && just allows you to run 2 commands in one line, so it says hey, use this HOST var and still run the "react-scripts start" like normal.

Note: You can also use this to change the PORT if it is already used by another script or node program in the same way:

"scripts":{
    "start":"export PORT=3002 && react-scripts start",
    "build": "react-scripts build",
        //...etc...
}

Happy coding!

« Back