Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen EADDRNOTAVAIL error in Node.js

I installed Nginx and Node.js in my server.

When I try run my node.js file, I get an error:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: listen EADDRNOTAVAIL
    at errnoException (net.js:614:11)
    at Array.0 (net.js:689:28)
    at EventEmitter._tickCallback (node.js:192:40)

How can I fix this problem?

Thanks!

like image 582
thuan leminh Avatar asked May 30 '12 03:05

thuan leminh


2 Answers

I had this issue, and I thought it was a port issue, but it turned out to be an IP issue.

I had my HOST environmental variable pointing to a static IP, and my Node server was listening to that address. At some point when my router recycled, it started assigning my machine a new IP, and I started getting the error you posted. It was fixed by changing the address my server was listening to.

My config:

app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || '0.0.0.0');

http.createServer(app).listen(app.get('port'), app.get('host'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Make sure your host is hitting your machine. In any case, the loopback, as @miltonb suggested, should always work:

app.set('host', '127.0.0.1');
like image 66
User 1058612 Avatar answered Oct 20 '22 02:10

User 1058612


I think you need a bit more context like some of your code. I am guessing you are starting to listen on a web server or a socket? Based on that assumption, I get something similar when I run a basic web server on my test server unless I run using localhost.

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                   ^
Error: listen EADDRNOTAVAIL
    at errnoException (net.js:670:11)
    at Array.0 (net.js:756:28)
    at EventEmitter._tickCallback (node.js:190:38)

Try changing the [hostname] parameter to localhost:

var http = require('http');
http.createServer( function ).listen(8000, '127.0.0.1');
like image 7
miltonb Avatar answered Oct 20 '22 02:10

miltonb