node.js 节点 js 侦听简单 httpserver 的 EADDRINUSE 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9204706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:08:52  来源:igfitidea点击:

node js listen EADDRINUSE error for simple httpserver

node.jshttpserver

提问by riship89

I have simple node js http server.

我有简单的节点 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);

If I run

如果我跑

node basicserver.js 

I get

我得到

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)

I have seen thispost, but that post seems to be specific to TCP server and not http server. Could anyone please help.

我看过这篇文章,但那篇文章似乎特定于 TCP 服务器而不是 http 服务器。任何人都可以请帮忙。

回答by Farid Nouri Neshat

The port you are listening to is already being listened by another process. In this case I got a feeling that it is your self. You can do a ps aux | grep nodeand then using kill <pid>kill your node process. Beside that you can also try another port.

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

--Update--

- 更新 -

In case if you want to find which process is listening, you can use netstat -lpn( -lis to find out the listening ports, -pis to include the process name and pid, -nis 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.

如果你想找到正在监听的进程,可以使用netstat -lpn(-l是找出监听端口,-p是包含进程名和pid,-n是不解析主机名,否则会很慢),到找到正在侦听不同端口的进程。如果有太多,你可以做netstat -lnp | grep :8888

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

也可以使用, fuser 8888/tcp,它会向您显示进程pid,并且添加-k将终止进程,这是有史以来最快的方式。

I realized this two commands only work in linux.

我意识到这两个命令仅适用于linux

回答by Pablo Cegarra

To kill all the experiments on my machine...

杀死我机器上的所有实验......

 killall node

回答by Anush

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)

当我遇到此错误时,我使用 Windows PowerShell 终止了该进程(因为我使用的是 Windows)

  1. open the windows powershell
  2. type psand then you can get list of processes
  3. find the process named node, and note the Id
  4. type Stop-process <Id>
  1. 打开 Windows PowerShell
  2. 输入ps然后你可以得到进程列表
  3. 找到名为 的进程node,并记下Id
  4. 类型 Stop-process <Id>

I think that it is help for windows users.

我认为这对 Windows 用户有帮助。

回答by 3k-

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

当您尝试运行服务器时可能会出现同样的问题,但您没有 root 权限。

回答by probie

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.

我也遇到了 EADDRINUSE 127.0.0.1 的这个问题。检查节点进程后,我将服务器 ip 更改为“本地主机”。这样做之后,它就开始了。希望这可以帮助。

回答by FreeStyler

On my Linux Mintonly kill by PIDwork for me:

关于我Linux Mint唯一PID对我来说有效的杀戮:

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

netstat -nltp | grep 8080- 显示PID使用8080端口,我的输出:

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

tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 27599/node-PID节点开发服务器27599在我的情况下

and then kill:

然后杀死:

kill -9 <PID>

kill -9 <PID>

kill -9 27599

kill -9 27599

Preamble:

前言:

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

不知道什么是错误,但我正在启动我的 WebPack 开发服务器,例如npm run dev

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

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

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

当打断他时 - 在终端中Ctrl+C并再次启动它 - 他会导致同样的错误Error: listen EADDRINUSE 127.0.0.1:8080,但在它完美运行之前一直存在。

killall nodenot helped.

killall node没有帮助。

回答by Anthony

For Windows users: in Powershell, you can use:

对于 Windows 用户:在 Powershell 中,您可以使用:

Stop-Process -Name "ProcessName" -Force

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

在这种情况下"ProcessName"将是"node".

More info here: How to kill a process in windows

更多信息:如何在 Windows 中终止进程