Search code examples
reactjscreate-react-app

Not using 'localhost' in HOST for development with create-react-app


My problem is quite simple: I would like to avoid using localhost at the end of my HOST env variable with create-react-app. However it seems that if the URL doesn't end with .localhost, the script will try to resolve the URL against a DNS server.

I would like to avoid that and just use the same URL domain as my backend server is using, to avoid CORS problems (and I wish not to configure my backend to allow CORS because that's not how the production infrastructure is).

Thanks :)


Solution

  • If you want to use some custom domain locally, without resolving it agains a DNS server, you can add that domain to your hosts file.

    Location of hosts file on Windows:

    C:\Windows\System32\drivers\etc\hosts
    

    Location of hosts file on Mac:

    /etc/hosts
    

    You can modify the hosts file by adding the following line to it:

    127.0.0.1       yourcustomdomain.com
    

    This will bind yourcustomdomain.com to your local IP. Now you can use yourcustomdomain.com in your create-react-app.