Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting `getAddrInfo ENOTFOUND localhost` on running simple node server even though localhost is present in /etc/hosts (MacOS El Capitan)

I have a very weird thing happening while setting up and running a normal node js server.

When I run node server.js, I get following error:

Server running at http://localhost:1337/
events.js:160
  throw er; // Unhandled 'error' event
  ^
Error: getaddrinfo ENOTFOUND localhost
       at errnoException (dns.js:28:10)
       at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

When I open http://localhost:1337/ in my browser, it says page does not exist.

When I run sudo node server.js, it runs fine without any error and I can access my site at https://localhost:1337

Here is my /etc/hosts file:

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

My server.js:

// Require what we need
var http = require("http");
// Build the server
var app = http.createServer(function(request, response {
    response.writeHead(200, {
        "Content-Type": "text/plain"
    });
response.end("Hello world!\n");
});
// Start that server, baby
app.listen(1337, "localhost");
console.log("Server running at http://localhost:1337/");

Other Details

  1. OS: Mac OS (El Capitan)
  2. Node JS version: 6.10.3 (installed globally)
  3. NPM version: 4.5.0 (installed globally)

Following are my questions

  1. Why is the above server.js file working with admin access but not as a normal user?
  2. Does globally installed node only work with admin access?
  3. Is there anything else in Mac OS which can affect DNS lookup other than /etc/hosts? I have already flushed my DNS cache and reboot my OS just to make sure that older DNS lookup is not being used.
like image 601
yeshashah Avatar asked Oct 20 '25 10:10

yeshashah


1 Answers

I have fixed this same error by editing the file /etc/hosts

Added the lines:

##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
like image 139
Yosvel Quintero Arguelles Avatar answered Oct 22 '25 02:10

Yosvel Quintero Arguelles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!