40
votes

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!

11
What port is your node file setup to listen on?srquinn

11 Answers

52
votes

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');
7
votes

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');
7
votes

I was getting the same error, and then I changed the port and worked

5
votes

Could it be that node was already running a server? I received a similar error and found this while troubleshooting. Shutting down the previous node server solved my problem.

3
votes

For me, the issue was that the IP that I wrote simply didn't exists :) usually at home I have a 192.168.x.x IP, and after a restart I had a different address ... when I tried to gulp serve my app - it had a config with the old IP ....

as always 127.0.0.1 will work, but when you want to verify your website with other devices, you want to use the external IP 192.168.x.x ... or similar.

1
votes

Most of the time it would be the IP address or the hostname. The reason being, node js takes that as the key item to start the server. if there is a conflict or incorrect ip address, it cribs with this error

Error: listen EADDRNOTAVAIL

hope this helps.

1
votes

For me , i had the same error , and when i check my configuration , i found that host=127.0.0.0 which raises error because it should be 127.0.0.1 instead of 127.0.0.0

0
votes
var express = require('express');

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || 'localhost');

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

works for me.
it also generic for debug by localhost and your local ip without any changes.

0
votes

This also will occur while 'ip addr add i:p:v:6' is still in limbo or something (just executed/ing), and the interface is I presume not totally ready to listen and still busy adding the new ipv6 address.

Using an execSync [see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options] to call 'sleep' for a 2 second pause seems to allow listen to not error with Error: listen EADDRNOTAVAIL.

[It's really a bug I think in NodeJS 7.4.0 on Ubuntu because this doesn't happen with ipv4 address creation at the instant before usage/listen connection.]

0
votes

Check your ip ipconfig adress and add any port 3004 for example. Code below finally work for me. In case if you want to access this server from other device in your network. Or just set local host ip to 127.0.0.1 and get access from your device.

var server = require('http').createServer(app);
server.listen(3004, '192.168.x.x', function () {
  console.log("Listening on port 3000!");
});
-1
votes

In my case, I fixed it by checking the directory /tasks/options

I found connect.js file which had static IP address which was incorrect. I just changed it to the correct one and it worked.