Node.js Error Message “getaddrinfo ENOTFOUND localhost” Solution

Jacob Knopf
The Startup
Published in
3 min readJun 5, 2020

Today I want to go over a quick fix to an error that had me very confused when I first started my program at Flatiron. If you try to run your JSON server and see this error message “getaddrinfo ENOTFOUND localhost,” it’s happening because Webpack cannot find your localhost address. In my case one line from the Hosts file was missing on my computer and I had to figure out how to add it back in. Thankfully the solution is super easy, and can literally be solved with a few lines of code. This is what the error message looks like.

Scary right? Here’s what you need to do to get that server up and running! First enter this command in the terminal.

sudo nano /etc/hosts

Next enter your admin password, and you’ll be brought to the Nano text editor. The UI should look something like this.

https://www.imore.com/how-edit-your-macs-hosts-file-and-why-you-would-want#how-to-edit-the-hosts-file

On my computer the line above that reads “127.0.0.1 localhost” was the one that was missing for some reason. Type this in the editor.

127.0.0.1 localhost

After you add that to the Hosts file, save your changes and get out of there before you break anything important. Hit Control + X to exit the Nano text editor when you’re finished. Voila! Try running your JSON server again. You should now be able to start it without any issues. This was such a huge win for me when I finally figured out what was causing the error, and honestly a little frustrating that it only took a few lines of code to solve the problem. This ordeal did reinforce a valuable lesson for me, however. Coding is all about the journey.

Once you’ve finished building something and gotten everything to work, it’s easy to look at your source code and see the progression from one method to another. What gets lost for me sometimes is the process itself. Sure in the end it all makes sense, but there will be times you’ll spend three days stuck on an absurd error, and the answer will end up being two lines of simple code you just didn’t know about yet. In those situations I often find myself saying: “why was that so hard to figure out?” But that’s why we code, and that’s the only way to learn and be prepared for the next programming catastrophe that will inevitably present itself when you least expect it.

--

--

Jacob Knopf
The Startup

QA Lead • Technical Writer • Content Designer • Developer Evangelist