Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm start use hostname instead of localhost on Linux #2954

Closed
guoyunhe opened this issue Aug 14, 2017 · 6 comments
Closed

npm start use hostname instead of localhost on Linux #2954

guoyunhe opened this issue Aug 14, 2017 · 6 comments

Comments

@guoyunhe
Copy link

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.

@Timer
Copy link
Contributor

Timer commented Aug 14, 2017

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.

@guoyunhe
Copy link
Author

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

@Timer Timer closed this as completed Aug 14, 2017
@Timer
Copy link
Contributor

Timer commented Aug 14, 2017

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

@guoyunhe
Copy link
Author

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",
    ...
  }
}

@Timer
Copy link
Contributor

Timer commented Aug 14, 2017

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...

@mcmillhj
Copy link

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

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants