11

我有简单的节点 js http 服务器。

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

如果我跑

node basicserver.js 

我明白了

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:642:11)
    at Array.0 (net.js:743:26)
    at EventEmitter._tickCallback (node.js:192:40)

我看过这篇文章,但那篇文章似乎特定于 TCP 服务器而不是 http 服务器。谁能帮忙。

4

7 回答 7

27

您正在侦听的端口已被另一个进程侦听。在这种情况下,我有一种感觉,那就是你自己。你可以做一个ps aux | grep node然后使用kill <pid>杀死你的节点进程。除此之外,您还可以尝试其他端口。

- 更新 -

In case if you want to find which process is listening, you can use netstat -lpn ( -l is to find out the listening ports, -p is to include the process name and pid, -n is to not to resolve host names, or else it will be slow), to find the processes that are listening on different ports. If there was too many, you can do netstat -lnp | grep :8888.

Also can use, fuser 8888/tcp, which will show you the process pid and also adding -k will kill the process, fastest way ever.

I realized this two commands only work in linux.

于 2012-02-09T03:17:15.647 回答
6

To kill all the experiments on my machine...

 killall node
于 2016-10-26T17:26:58.347 回答
3

The port you are listening to is already being listened by another process.

When I faced to this error I killed the process using Windows PowerShell (because I used Windows)

  1. open the windows powershell
  2. type ps and then you can get list of processes
  3. find the process named node, and note the Id
  4. type Stop-process <Id>

I think that it is help for windows users.

于 2018-12-12T11:06:12.463 回答
2

Same problem may occur when you try to run the server, but you don't have root privileges.

于 2012-11-05T16:40:07.470 回答
1

On my Linux Mint only kill by PID work for me:

netstat -nltp | grep 8080 - shows PID of using 8080 port, my output:

tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 27599/node - PID of node dev server is 27599 in my case

and then kill:

kill -9 <PID>

kill -9 27599

Preamble:

Don't know what a bug, but I am start my WebPack dev server like npm run dev:

webpack-dev-server --hot --host int.loc --port 8080

And when interrupt him - in terminal by Ctrl+C and start it again - he causing same error Error: listen EADDRINUSE 127.0.0.1:8080, but all time before it work perfect.

killall node not helped.

于 2018-10-15T10:03:01.780 回答
0

I also ran into this issue with EADDRINUSE 127.0.0.1. After checking for Node processes I changed the server ip to 'localhost'. After doing this it started right up. Hope this helps.

于 2018-03-09T17:40:32.190 回答
0

For Windows users: in Powershell, you can use:

Stop-Process -Name "ProcessName" -Force

In this case "ProcessName" would be "node".

More info here: How to kill a process in windows

于 2019-07-03T16:31:48.053 回答