Create-react-app: npm start use hostname instead of localhost on Linux

Created on 14 Aug 2017  路  6Comments  路  Source: facebook/create-react-app

My laptop uses openSUSE Linux with hostname "Guos-Kudu". When I created a react app and run npm start, it opens http://guos-kudu:3000 in browser. When I visit http://localhost:3000 it cannot be accessed.

Some HTTP API only allows localhost to access. Or the API administrator only enabled localhost in OAuth configuration. In this situation, a non-standard URL will have many troubles.

Even when I changed the hostname to localhost or remove it, react-scripts still looks for http://guos-kudu:3000 instead of localhost. Have no idea what caused this.

question

Most helpful comment

The HOST variable is probably set in your env. Try to unset it in your terminal session.

If that works, create a .env.local file which contains the following:

HOST=localhost

You can now run npm start without worrying about unsetting HOST.


To achieve the default CRA behavior, use 0.0.0.0 instead of localhost.

All 6 comments

The HOST variable is probably set in your env. Try to unset it in your terminal session.

If that works, create a .env.local file which contains the following:

HOST=localhost

You can now run npm start without worrying about unsetting HOST.


To achieve the default CRA behavior, use 0.0.0.0 instead of localhost.

@Timer Thanks! This works. I do not know why the $HOST variable is not updated after I changed my hostname and rebooted the system.

You might want to check your ~/.bashrc or similar (~/.profile, ~/.bash_profile, ~/.bash_profile, ~/.login, etc).

About .env file, it doesn't really support Linux system variable. Only those variables started with REACT_APP_ will be considered while others will be ignored. So I still have to write it in package.json:

{
  ...
  "scripts": {
    "start": "HOST=localhost react-scripts start",
    ...
  }
}

Ah yes, it does support HOST but doesn't overwrite it if it's already set -- my bad! So what you did is the correct fix but might get weird sharing between machines.

Maybe we can add a force overwrite...

You could add an entry in your /etc/hosts file for your hostname that points to 127.0.0.1 to get around this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  路  3Comments

stopachka picture stopachka  路  3Comments

Aranir picture Aranir  路  3Comments

adrice727 picture adrice727  路  3Comments

rdamian3 picture rdamian3  路  3Comments