node.js 节点/快递:EADDRINUSE,地址已在使用 - 杀死服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4075287/
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 13:43:20  来源:igfitidea点击:

Node / Express: EADDRINUSE, Address already in use - Kill server

node.js

提问by Skawful

I have a simple server running in node.js using connect:

我有一个使用 connect 在 node.js 中运行的简单服务器:

var server = require('connect').createServer();
//actions...
server.listen(3000);

In my code I have actual handlers, but thats the basic idea. The problem I keep getting is

在我的代码中,我有实际的处理程序,但这是基本思想。我不断遇到的问题是

EADDRINUSE, Address already in use

I receive this error when running my application again after it previously crashed or errors. Since I am not opening a new instance of terminal I close out the process with ctr + z.

在我的应用程序之前崩溃或出现错误后再次运行我的应用程序时,我收到此错误。由于我没有打开终端的新实例,因此我使用ctr + z.

I am fairly certain all I have to do is close out the server or connection. I tried calling server.close()in process.on('exit', ...);with no luck.

我相当肯定我所要做的就是关闭服务器或连接。我打过电话server.close()process.on('exit', ...);,没有运气。

采纳答案by Tor Valamo

process.on('exit', ..)isn't called if the process crashes or is killed. It is only called when the event loop ends, and since server.close()sort ofends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event...

process.on('exit', ..)如果进程崩溃或被杀死,则不会调用。它只能被称为当事件循环结束,而且由于server.close()某种目的的事件循环(它仍必须等待目前跑这里堆有),这是没有意义的把该退出事件中...

On crash, do process.on('uncaughtException', ..)and on kill do process.on('SIGTERM', ..)

崩溃时,执行process.on('uncaughtException', ..)和杀死执行process.on('SIGTERM', ..)

That being said, SIGTERM (default kill signal) lets the app clean up, while SIGKILL (immediate termination) won't let the app do anything.

话虽如此,SIGTERM(默认终止信号)让应用程序清理,而 SIGKILL(立即终止)不会让应用程序做任何事情。

回答by djburdick

You can also go the command line route:

你也可以走命令行路线:

ps aux | grep node

to get the process ids.

获取进程ID。

Then:

然后:

kill -9 PID

Doing the -9 on kill sends a SIGKILL (instead of a SIGTERM). SIGTERM has been ignored by node for me sometimes.

在 kill 时执行 -9 会发送 SIGKILL(而不是 SIGTERM)。SIGTERM 有时会被节点忽略。

回答by Mina Gabriel

First, you would want to know which process is using port 3000

首先,您想知道正在使用哪个进程 port 3000

sudo lsof -i :3000

this will list all PIDlistening on this port, once you have the PIDyou can terminate it with the following:

这将列出在此端口上侦听的所有PID,一旦您拥有PID,您就可以使用以下命令终止它:

kill -9 {PID}

回答by Sushil

I hit this on my laptop running win8. this worked.

我在运行 win8 的笔记本电脑上点击了这个。这有效。

Run cmd.exe as 'Administrator':

以“管理员”身份运行 cmd.exe:

C:\Windows\System32>taskkill /F /IM node.exe
SUCCESS: The process "node.exe" with PID 11008 has been terminated.

回答by Ayan

Check the PID i.e. id of process running on port 3000 with below command :

使用以下命令检查在端口 3000 上运行的进程的 PID,即 id:

lsof -i tcp:3000

It would output something like following:

它会输出如下内容:

COMMAND  PID   USER   FD   TYPE  DEVICE  SIZE/OFF NODE NAME
node     5805  xyz    12u  IPv6  63135    0t0     TCP  *:3000 (LISTEN)

Now kill the process using :

现在使用以下命令终止进程:

kill -9 5805

回答by alicanozkara

I found this solution, try it Give permission use sudo

我找到了这个解决方案,试试吧 授予使用权限 sudo

  sudo pkill node

回答by Shripad Krishna

Linux

Linux

Run psand determine the PID of your node process.

运行ps并确定节点进程的 PID。

Then, run sudo kill PID

然后,运行 sudo kill PID

Windows

视窗

Use tasklist to display the list of running processes:

使用 tasklist 显示正在运行的进程列表:

tasklist /O

Then, kill the node process like so (using the PID obtained from the tasklistcommand):

然后,像这样终止节点进程(使用从tasklist命令中获得的 PID ):

taskkill /pid PID

回答by floribon

Here is a one liner (replace 3000 with a port or a config variable):

这是一个单行(用端口或配置变量替换 3000):

kill $(lsof -t -i:3000)

回答by Kit

I was getting this error once and took many of the approaches here.

我曾经遇到过这个错误,并在这里采取了许多方法。

My issues was that I had two app.listen(3000);calls in the same app.js script.The first app.listen() succeeded where the second threw the error.

我的问题是我app.listen(3000);在同一个 app.js 脚本中有两个调用。第一个 app.listen() 在第二个抛出错误的地方成功了。

Another useful command I came across that helped me debug was sudo fuser -k 3000/tcpwhich will kill any rogue processes you might have started (some processes may restart, e.g. if run with forever.js, but it was useful for me).

我遇到的另一个帮助我调试的有用命令是sudo fuser -k 3000/tcp它会杀死您可能已经启动的任何流氓进程(某些进程可能会重新启动,例如,如果使用forever.js 运行,但它对我很有用)。

回答by Shahdat

For windows open Task Manager and find node.exe processes. Kill all of them with End Task.

对于 Windows,打开任务管理器并找到 node.exe 进程。用结束任务杀死他们所有人。

enter image description here

在此处输入图片说明