webpack-dev-server can't find localhost
Solution 1
Solved, turned out my hosts file (found under /etc/hosts on a mac) had one line missing and so webpack couldn't find localhost. I restored it to its default following these instructions and it now works!
Solution 2
Make sure your /etc/hosts file includes localhost.
Open /etc/hosts
$ sudo vi /etc/hosts
Copy and paste this, if it's missing:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
Solution 3
Did not work for me even after restoring the hosts file and trying with a different port. The way I made it to work was to give a custom host (127.0.0.1) and port (3001 or any other available) when starting the server:
webpack-dev-server --host 127.0.0.1 --port 3001
Solution 4
The most common reason for that error is that you have something else already using that port. Try starting it on another port.
webpack-dev-server --port 3001
Solution 5
Also check if /etc/hosts have 127.0.0.1 pointing to localhost as well.
lizzie-cd
Updated on June 03, 2021Comments
-
lizzie-cd about 3 years
I'm trying to get started with React using webpack, but keep running into an error when I try to set up webpack-dev-server. I ran
npm install webpack-dev-server
followed by
webpack-dev-server
and the command line returns the following:
events.js:154 throw er; // Unhandled 'error' event ^ Error: getaddrinfo ENOTFOUND localhost at errnoException (dns.js:26:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)
Any clues on what is happening here?
I don't really know how webpack works to try and search for the root of the problem, but I've also been developing a website using PHP at port 80 with MAMP, I don't know if this is maybe part of the issue?
-
lizzie-cd over 8 yearshey, i tried your suggestion but get the exact same error. is there another way to reconfigure it?
-
David Bradshaw over 8 yearsYes, but it depends on how you have webpack setup. Start with the node server js file and look for app.listern, then either hard code a new port number or work your way back through the code to where it's configured.
-
Nick Hingston about 6 yearsGreat stuff. My hosts was empty - oddly webpack-dev-server was working, but 'electron-webpack dev' was not!
-
crumbug almost 6 yearsSame here, my hosts was also mysteriously empty!