How to fix Error: listen EADDRINUSE while using no

2018-12-31 10:22发布

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 to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet, while the server is running.

The server is:

  net.createServer(function (socket) {
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
    console.log('connection request from: ' + socket.remoteAddress);
    socket.destroy();
  }).listen(options.port);

And the request:

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    sys.puts("State: " + this.readyState);

    if (this.readyState == 4) {
        sys.puts("Complete.\nBody length: " + this.responseText.length);
        sys.puts("Body:\n" + this.responseText);
    }
};

xhr.open("GET", "http://mywebsite.com");
xhr.send();

30条回答
心情的温度
2楼-- · 2018-12-31 11:07

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
查看更多
只靠听说
3楼-- · 2018-12-31 11:07

I had the same issue recently.

It means that the port is already being used by another application (express or other software)

In my case, I had accidentally run express on 2 terminals, so exiting the terminal using 'Ctrl + C' fixed things for me. (Run server from only one terminal)

Hope it helps others.

查看更多
高级女魔头
4楼-- · 2018-12-31 11:08

Another thing that can give this error, is two HTTP servers in the same node code. I was updating some Express 2 to Express 3 code, and had this...

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

// tons of shit.

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

And, it triggered this error.

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 11:08

For other people on windows 10 with node as localhost and running on a port like 3500, not 80 ...

What does not work:

killall    ?  command not found
ps -aux | grep 'node'     ?     ps:  user x unknown 

What shows information but still not does work:

 ps -aef | grep 'node'
 ps ax
 kill -9 61864

What does work:

Git Bash or Powershell on Windows

  net -a -o | grep 3500   (whatever port you are looking for) 

Notice the PID ( far right )
I could not get killall to work... so

  1. Open your task manager
  2. On processes tab , right click on Name or any column and select to include PID
  3. Sort by PID, then right click on right PID and click end task.

Now after that not so fun exercise on windows, I realized I can use task manager and find the Node engine and just end it.

FYI , I was using Visual Studio Code to run Node on port 3500, and I use Git Bash shell inside VS code. I had exited gracefully with Ctrl + C , but sometimes this does not kill it. I don't want to change my port or reboot so this worked. Hopefully it helps others. Otherwise it is documentation for myself.

查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 11:10

You should try killing the process that is listening on port 80.

Killall will kill all the node apps running. You might not want to do that. With this command you can kill only the one app that is listening on a known port.

If using unix try this command:

sudo fuser -k 80/tcp    
查看更多
明月照影归
7楼-- · 2018-12-31 11:10

In my case Apache HTTP Server was run on port 80 I solved it by issue the command as root

sudo killall httpd

Update

If Jenkin is installed and running on your Mac;

  1. You can check it with sudo lsof -i tcp:8080
  2. If Yes, and You want to stop Jenkins only once, run: sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
查看更多
登录 后发表回答