listen EADDRNOTAVAIL error in Node.js

前端 未结 11 699
陌清茗
陌清茗 2020-11-27 15:43

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 \'e         


        
相关标签:
11条回答
  • 2020-11-27 16:07

    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');
    
    0 讨论(0)
  • 2020-11-27 16:08

    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!");
    });
    
    0 讨论(0)
  • 2020-11-27 16:13
    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 讨论(0)
  • 2020-11-27 16:14

    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.

    0 讨论(0)
  • 2020-11-27 16:18

    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.

    0 讨论(0)
  • 2020-11-27 16:24

    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.

    0 讨论(0)
提交回复
热议问题