How to fix Error: listen EADDRINUSE while using nodejs?

前端 未结 30 3194
执念已碎
执念已碎 2020-11-22 06:44

If I run a server with the port 80, and I try to use xmlHTTPrequest i get this error: Error: listen EADDRINUSE

Why is it problem for nodejs, if I want t

相关标签:
30条回答
  • 2020-11-22 06:48

    This works for me (I'm using mac). Run this command

    lsof -PiTCP -sTCP:LISTEN

    This's going to display a list of ports that your syetem is using. Find the PID that your node is running

    COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    node      17269 hientrq   16u  IPv6 0xc42959c6fa30c3b9      0t0  TCP *:51524 (LISTEN)
    node      17269 hientrq   19u  IPv4 0xc42959c71ae86fc1      0t0  TCP localhost:1337 (LISTEN)
    

    and run kill -9 [YOUR_PID]

    0 讨论(0)
  • 2020-11-22 06:48

    EADDRINUSE means port of your nodejs app is already in use.

    • Now you have kill the process/app running on that port.
    • Find the process id of app by:

    lsof -i tcp:3000

    • Now u will get process id from this.
    • Run this:

    kill -9 processId

    0 讨论(0)
  • 2020-11-22 06:48

    For windows users execute the following command in PowerShell window to kill all the node processes.

    Stop-Process -processname node
    
    0 讨论(0)
  • 2020-11-22 06:49

    EADDRINUSE means that the port(which we try to listen in node application) is already being used. In order to overcome, we need to identify which process is running with that port.

    For example if we are trying to listen our node application in 3000 port. We need to check whether that port is already is being used by any other process.

    step1:

    $sudo netstat -plunt |grep :3000
    

    That the above command gives below result.

    tcp6       0      0 :::3000                 :::*                    LISTEN      25315/node
    

    step2:

    Now you got process ID(25315), Kill that process.

    kill -9 25315
    

    step3:

    npm run start
    

    Note: This solution for linux users.

    0 讨论(0)
  • 2020-11-22 06:51

    EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

    So, in your case, there must be running a server on port 80 already.

    If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

    You should check for the listening event like this, to see if the server is really listening:

    var http=require('http');
    
    var server=http.createServer(function(req,res){
        res.end('test');
    });
    
    server.on('listening',function(){
        console.log('ok, server is running');
    });
    
    server.listen(80);
    
    0 讨论(0)
  • 2020-11-22 06:51

    While killing the NODE_PORT, it might kill your chrome process or anything that is listening to the same port, and that's annoying.

    This shell script may be helpful - in my case the port is 1337 but you can change it anytime

    # LOGIC
    
    CHROME_PIDS=`pidof chrome`
    PORT_PIDS=`lsof -t -i tcp:1337`
    
    for pid in $PORT_PIDS
    do
    
    if [[ ${CHROME_PIDS} != *$pid* ]];then
    
        # NOT FOUND IN CHROME PIDS
    
        echo "Killing $pid..."
        ps -p "$pid"
    
        kill -kill "$pid"
        fi
    
    done
    
    sails lift
    # OR 'node app' OR whatever that starts your node
    
    exit
    
    0 讨论(0)
提交回复
热议问题