如何解决错误:使用nodejs时监听EADDRINUSE?

JavaScript

GilTomL

2020-03-13

如果我使用端口80运行服务器,并且尝试使用xmlHTTPrequest,则会出现此错误:Error: listen EADDRINUSE

如果我要在端口80上运行服务器时发出请求,为什么nodejs会出现问题?对于网络浏览器来说,这不是问题:在服务器运行时,我可以在Internet上冲浪。

服务器是:

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

并要求:

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();

第1445篇《如何解决错误:使用nodejs时监听EADDRINUSE?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

24个回答
凯小胖 2020.03.13

For windows users execute the following command in PowerShell window to kill all the node processes.

Stop-Process -processname node
老丝GO 2020.03.13

Two servers can not listen on same port, so check out if other server listening on same port, also check out for browser sync if its running on same port

Jim米亚西里 2020.03.13

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
神奇Davaid 2020.03.13

Seems there is another Node ng serve process running. Check it by typing this in your console (Linux/Mac):

ps aux|grep node

and quit it with:

kill -9 <NodeProcessId>

OR alternativley use

ng serve --port <AnotherFreePortNumber>

to serve your project on a free port of you choice.

凯JinJin 2020.03.13

On Debian i found out to run on port 80 you need to issue the command as root i.e

sudo node app.js

I hope it helps

LEY小卤蛋 2020.03.13

I have the same problem too,and I simply close the terminal and open a new terminal and run

node server.js

again. that works for me, some time just need to wait for a few second till it work again.

But this works only on a developer machine instead of a server console..

前端LEYLEY 2020.03.13

Error: listen EADDRINUSE means the port which you want to assign/bind to your application server is already in use. You can either assign another port to your application.

Or if you want to assign the same port to the app. Then kill the application that is running at your desired port.

For a node application what you can try is, find the process id for the node app by :

ps -aux | grep node

After getting the process id, do

kill process_id
梅番长 2020.03.13

I have seen this error before (in node) with http.client, and as I recall, the problem had to do with not initializing the httpClient or setting bad options in the httpClient creation and/or in the url request.

凯小卤蛋 2020.03.13

There is a way to terminate the process using Task Manager:

Note that this solution is for Windows only

  1. Go to the Task Manager (or using the shortcut Ctrl + Shift + Esc)

  2. On "Background Processes", find "Node.js" processes and terminate them (Right-click them and choose "End Task")

在此处输入图片说明

  1. Now you should be able to start again
猿西门Tom 2020.03.13

EADDRINUSE表示您的nodejs应用程序的端口已在使用中。

  • 现在,您已经杀死了在该端口上运行的进程/应用程序。
  • 通过以下方式查找应用的进程ID:

lsof -i tcp:3000

  • 现在您将从中获取进程ID。
  • 运行这个:

杀死-9 processId

卡卡西理查德 2020.03.13

在下面的命令中替换您的portNumber

sudo lsof -t -i tcp:portNumber | xargs kill -9
西里小哥 2020.03.13

当您要在其上运行应用程序的端口上运行任何进程时,就会出现此错误。

如何获取在该端口上运行的进程=>命令:sudo netstat -ap | grep:3000

输出:您将获得使用该端口的过程信息

tcp 0 0 IP地址:3000 LISTEN 26869 / node

现在您可以杀死该进程sudo kill -9 26869

神乐Mandy小哥 2020.03.13
lsof -i:3000;
kill -9 $(lsof -t -i:3000);
// 3000 is a your port
// This "lsof -i:3000;" command will show PID 
kill PID 
ex: kill 129393
HarryItachi 2020.03.13

尝试这两个命令,它将停止所有节点进程。

killall 9 node
pkill node
npm start 
A小宇宙 2020.03.13

须藤杀死$(须藤lsof -t -i:80)

为了杀人

sudo kill -9 $(sudo lsof -t -i:80)

使用以上cmd杀死特定端口,然后运行服务器

null 2020.03.13

EADDRINUSE表示该端口(我们试图在节点应用程序中监听)已被使用。为了克服这个问题,我们需要确定哪个进程正在该端口上运行。

例如,如果我们尝试在3000端口监听我们的节点应用程序。我们需要检查该端口是否已被任何其他进程使用。

步骤1:

$sudo netstat -plunt |grep :3000

上面的命令给出了以下结果。

tcp6       0      0 :::3000                 :::*                    LISTEN      25315/node

第2步:

现在您获得了进程ID(25315),请杀死该进程。

kill -9 25315

第三步:

npm run start

注意:此解决方案适用于linux用户。

Green留姬 2020.03.13

这对我有效(我使用的是Mac)。运行此命令

lsof -PiTCP -sTCP:LISTEN

这将显示您的系统正在使用的端口列表。找到PID您的节点正在运行

COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node      17269 hientrq   16u  IPv6 0xc42959c6fa30c3b9      0t0  TCP *:51524 (LISTEN)
node      17269 hientrq   19u  IPv4 0xc42959c71ae86fc1      0t0  TCP localhost:1337 (LISTEN)

并运行 kill -9 [YOUR_PID]

小小仲羽 2020.03.13

您的应用程序已在该端口8080上运行。使用此代码杀死端口并再次运行您的代码

sudo lsof -t -i tcp:8080 | xargs kill -9
GO飞云 2020.03.13

在控制器环境下,您可以使用:

pkill node 在运行脚本之前,应该先完成这项工作。

请记住,此命令将终止所有node进程,如果您有一个仅运行一个实例的容器,那么这可能是正确的,因为您拥有可以保证这一点的环境。

在任何其他情况下,建议您使用命令通过编程查找某个进程ID或名称来杀死它。就像您的进程被称为node-server-1一样,您可以这样做pkill node-server-1

理解以下资源可能会很有用:https : //www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/

梅蛋蛋 2020.03.13

错误原因:您正在尝试使用忙port number

Windows / Mac的两种可能的解决方案

  1. 当前使用的免费端口号
  2. 为您的当前程序选择另一个端口号


1.空闲端口号

视窗

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

在此处输入图片说明

苹果电脑

您可以尝试netstat

netstat -vanp tcp | grep 3000

对于OSX El Capitan和更高版本(或者如果您的netstat不支持-p),请使用lsof

sudo lsof -i tcp:3000

如果这不能解决您的问题,则Mac用户可以参考有关此问题的完整讨论,在Mac上查找(并终止)进程以锁定端口3000


2.更改端口号?

视窗

set PORT=5000

苹果电脑

export PORT=5000
小宇宙神无 2020.03.13

您应该尝试终止正在侦听端口80的进程。

Killall将杀死所有正在运行的节点应用程序。您可能不想这样做。使用此命令,您只能杀死正在已知端口上监听的一个应用程序。

如果使用unix,请尝试以下命令:

sudo fuser -k 80/tcp    
Green理查德 2020.03.13

killall -9 nodePatrick所建议的上述方法按预期工作,并且可以解决问题,但是您可能希望阅读此答案的编辑部分,以了解为什么kill -9不是最佳方法。

最重要的是,您可能希望针对单个进程,而不是盲目地杀死所有活动进程。

在这种情况下,首先获取在该端口上运行的进程的进程ID(PID)(例如8888):

lsof -i tcp:8888

这将返回类似:

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

然后就做(ps-实际上不行。请继续阅读下面的内容):

kill -9 57385

您可以在这里阅读更多有关此的内容

编辑:我今天在阅读一个相当相关的主题,偶然发现了一个有趣的话题,为什么我不应该kill -9进行处理

通常,您应该kill -9之前使用kill -15以使目标进程有机会在其自身之后进行清理。(进程不能捕获或忽略SIGKILL,但是它们可以并且经常捕获SIGTERM。)如果您没有给该进程完成其工作和清理的机会,它可能会留下损坏的文件(或其他状态)重新启动后将无法理解。

因此,如上所述,您最好使用以下方法终止上述过程:

kill -15 57385

编辑2:正如在此处的注释中多次提到的,此错误是由于没有正常退出进程而导致的。这意味着许多人使用CTRL + Z退出节点命令(或其他任何命令)停止正在运行的进程的正确方法是发出CTRL + C命令,该命令执行干净退出。

以正确的方式退出进程将在关闭时释放该端口。这样一来,您就可以重新启动该过程,而不必担心自己被杀死后才能再次运行它。

蛋蛋LEY 2020.03.13

抬起头来,Skype有时会在端口80上侦听,因此,如果您尝试从Node.js或任何其他应用程序侦听端口80,则会导致此错误。

您可以通过访问选项并单击高级->连接->使用端口80(取消选中此选项)来在Skype中关闭该行为。

关闭Skype端口80的使用

PS进行更改后,请不要忘记重新启动Skype!

null 2020.03.13

对我真正的帮助是:

killall -9 node

但这会杀死系统进程。

ps ax

您可以检查它是否有效。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android