简体   繁体   中英

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!

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

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

我遇到了同样的错误,然后我更改了端口并开始工作

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.

For me, the issue was that the IP that I wrote simply didn't exists :) usually at home I have a 192.168.xx 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.xx ... or similar.

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.

对我来说,我遇到了同样的错误,当我检查我的配置时,我发现host=127.0.0.0会引发错误,因为它应该是127.0.0.1而不是127.0.0.0

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.

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.]

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!");
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM