sanjihan
sanjihan

Reputation: 6004

ng serve error: getaddrinfo ENOTFOUND localhost

I was getting this error:

getaddrinfo ENOTFOUND localhost
Error: getaddrinfo ENOTFOUND localhost
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

when running ng serve. After web lookup, the proposed answer to this problem is to run this command instead:

ng serve --port 4200 --host 0.0.0.0

Unfortunately, the instructions came without explanation.

This works and the Angular project compiled successfully. I am not sure why. Can someone explain why this works and what has happened in the background? Thanks

Upvotes: 12

Views: 20454

Answers (6)

Saad
Saad

Reputation: 366

For the person who use windows.
I have the same problem in angular.

  1. Go to C:\Windows\System32\drivers\etc\hosts.txt.
  2. Add localhost to 127.0.0.1 line
    ex :
    # localhost name resolution is handled within DNS itself.
    #   127.0.0.1       localhost
    #   ::1             localhost
    127.0.0.1 localhost

Upvotes: 0

Cyril
Cyril

Reputation: 162

In my case, it was a corrupted 'hosts' file. A nightly update on my MAC OS caused it. This error made no sense at first and all conditions were misleading:

  1. I ran 'ng serve' all day without error until suddenly vscode started throwing that error late in the day!
  2. I haven't updated the hosts file in a year.
  3. cat /etc/hosts --> 127.0.0.1 localhost (NO ERROR!)
  4. ls -rtl /etc --> hosts file modified in the morning!
  5. vi /etc/hosts --> bunch of ASCII characters that were not visible using cat command were now visible and corrupted the file!
  6. I deleted the unwanted characters and it solved the issue.

Upvotes: 0

Matan Shushan
Matan Shushan

Reputation: 1274

For those of you who work with docker, I had this issue because my docker was stuck, reset the docker application and everything was working again.

Upvotes: 0

Ravi Anand
Ravi Anand

Reputation: 5524

solution for Ubuntu users

--host option does not work until there is an explicit entry in hosts file for your Operating system. I solved it adding a host entry inside hosts' file under ~/etc/hosts` on ubuntu OS.

to edit hosts file

sudo vim /etc/hosts // you could use nano or something else as per your preferred editor

entry example inside hosts

example Hosts snip

Upvotes: 8

Dalorzo
Dalorzo

Reputation: 20014

The issue for me was the host entry:

Since I was on MAC I cmd+space bar type terminal then

sudo nano /private/etc/hosts

add the localhost entry ^ + O to save and ^ + x to exit

and then my problem was resolved

Upvotes: 4

jyotisankar
jyotisankar

Reputation: 144

Hi I was having the same issue, my configuration is virtual host was not for localhost. At the same time this issue comes when there is conflict between your global Angular CLI version with local Angular CLI version. I tried with ng serve --port 4200 --host 127.0.0.1 that works for me.

Thanks

Upvotes: 8

Related Questions