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 10:45

Error reason: You are trying to use the busy port number

There are two options to resolve this issue in Windows/Mac

  1. Free currently used port number
  2. Select another port number for your current program

1. Free Port Number

Windows

1. netstat -ano | findstr :4200
2. taskkill /PID 5824 /F

enter image description here

Mac

You can try netstat

netstat -vanp tcp | grep 3000

For OSX El Capitan and newer (or if your netstat doesn't support -p), use lsof

sudo lsof -i tcp:3000

if this does not resolve your problem, Mac users can refer to complete discussion about this issue Find (and kill) process locking port 3000 on Mac

2. Change Port Number?

Windows

set PORT=5000

Mac

export PORT=5000
查看更多
临风纵饮
3楼-- · 2018-12-31 10:46

I would prefer doing

killall -15 node

because, kill -15 gives process a chance to cleanup itself. Now, you can verify by

ps aux | grep node

Note: If you don't give process a chance to finish what it is currently doing and clean up, it may lead to corrupted files

查看更多
梦醉为红颜
4楼-- · 2018-12-31 10:50

The option which is working for me :

Run:

ps -ax | grep node

You'll get something like:

 8078 pts/7    Tl     0:01 node server.js
 8489 pts/10   S+     0:00 grep --color=auto node    
 kill -9 8078
查看更多
明月照影归
5楼-- · 2018-12-31 10:51

Your application is already running on that port 8080 . Use this code to kill the port and run your code again

sudo lsof -t -i tcp:8080 | xargs kill -9
查看更多
无色无味的生活
6楼-- · 2018-12-31 10:51

In below command replace your portNumber

sudo lsof -t -i tcp:portNumber | xargs kill -9
查看更多
其实,你不懂
7楼-- · 2018-12-31 10:52

The aforementioned killall -9 node, suggested by Patrick works as expected and solves the problem but you may want to read the edit part of this very answer about why kill -9 may not be the best way to do it.

On top of that you might want to target a single process rather than blindly killing all active processes.

In that case, first get the process ID (PID) of the process running on that port (say 8888):

lsof -i tcp:8888

This will return something like:

COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node     57385   You   11u  IPv6 0xac745b2749fd2be3      0t0  TCP *:ddi-tcp-1 (LISTEN)

Then just do (ps - actually do not. Please keep reading below):

kill -9 57385

You can read a bit more about this here.

EDIT: I was reading on a fairly related topic today and stumbled upon this interesting thread on why should i not kill -9 a process.

Generally, you should use kill -15 before kill -9 to give the target process a chance to clean up after itself. (Processes can't catch or ignore SIGKILL, but they can and often do catch SIGTERM.) If you don't give the process a chance to finish what it's doing and clean up, it may leave corrupted files (or other state) around that it won't be able to understand once restarted.

So, as stated you should better kill the above process with:

kill -15 57385

EDIT 2: As noted in a comment around here many times this error is a consequence of not exiting a process gracefully. That means, a lot of people exit a node command (or any other) using CTRL+Z. The correct way of stopping a running process is issuing the CTRL+C command which performs a clean exit.

Exiting a process the right way will free up that port while shutting down. This will allow you to restart the process without going through the trouble of killing it yourself before being able to re-run it again.

查看更多
登录 后发表回答