如何修正错误:使用nodejs时听EADDRINUSE ?

时间:2021-07-29 23:54:42

If I run a server with the port 80, and I try to use xmlHTTPrequest i get this error: Error: listen EADDRINUSE

如果我运行一个带有80端口的服务器,并尝试使用xmlHTTPrequest,我就会得到这个错误: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.

当我在端口80上运行服务器时,如果我想做一个请求,为什么nodejs会有问题?对于网络浏览器来说,这不是问题:我可以在服务器运行时上网冲浪。

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

28 个解决方案

#1


307  

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

EADDRINUSE意味着listen()试图将服务器绑定到的端口号已经在使用中。

So, in your case, there must be running a server on port 80 already.

因此,在您的示例中,必须已经在端口80上运行服务器。

If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

如果在这个端口上运行另一个webserver,则必须放置节点。服务器后面的js并通过它代理它。

You should check for the listening event like this, to see if the server is really listening:

您应该检查一下像这样的监听事件,看看服务器是否真的在监听:

var http=require('http');

var server=http.createServer(function(req,res){
    res.end('test');
});

server.on('listening',function(){
    console.log('ok, server is running');
});

server.listen(80);

#2


372  

What really helped for me was:

真正对我有帮助的是:

killall -9 node

But this will kill a system process.

但这将扼杀系统进程。

With

ps ax

you can check if it worked.

你可以检查一下是否有效。

#3


185  

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.

前面提到的killall -9节点,由Patrick建议可以按照预期工作并解决问题,但是您可能希望阅读本文的编辑部分,了解为什么kill -9可能不是最好的方法。

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):

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

lsof -i tcp:8888

lsof - 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):

然后就去做(ps -实际上不做。请继续阅读下文):

kill -9 57385

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.

编辑:我今天阅读了一个相当相关的话题,偶然发现了一个有趣的话题:为什么我不应该杀死-9个进程。

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.

通常,您应该在kill -9之前使用kill -15,以给目标进程一个自己清理的机会。(进程不能捕获或忽略SIGKILL,但它们可以而且经常捕获SIGTERM。)如果您不给流程一个机会来完成它正在做的事情并进行清理,它可能会在重新启动后留下损坏的文件(或其他状态),使其无法理解。

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

因此,如前所述,您最好以以下方式终止上述过程:

kill -15 57385

杀-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.

编辑2:正如这里的一个评论所指出的,这个错误很多时候是由于没有优雅地退出进程而导致的。这意味着,很多人使用CTRL+Z退出节点命令(或其他命令)。停止运行进程的正确方法是发出执行干净退出的CTRL+C命令。

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.

以正确的方式退出进程将释放该端口,同时关闭该端口。这将允许您重新启动流程,而不必在重新运行之前自己将其杀死。

#4


52  

** BEFORE DOWNVOTING - Please READ the answer. IT IS RELEVANT! If you are going to downvote this, leave a comment why you think it isn't relevant.

**在投反对票前-请阅读答案。这是有关!如果你打算投反对票,请留下评论,说明为什么你认为这无关紧要。


Just a head's up, Skype will sometimes listen on port 80 and therefore cause this error if you try to listen on port 80 from Node.js or any other app.

如果你试着从Node监听端口80,Skype有时会在端口80上监听,因此会导致这个错误。js或其他应用。

You can turn off that behaviour in Skype by accessing the options and clicking Advanced -> Connection -> Use port 80 (Untick this)

您可以通过访问选项并单击Advanced ->连接->使用端口80来关闭Skype中的这种行为(取消此选项)

如何修正错误:使用nodejs时听EADDRINUSE ?

#5


29  

You should try killing the process that is listening on port 80.

您应该尝试终止监听端口80的进程。

Killall will kill all the node apps running. You might not want to do that. With this command you can kill only the one app that is listening on a known port.

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

If using unix try this command:

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

sudo fuser -k 80/tcp    

#6


13  

Another thing that can give this error, is two HTTP servers in the same node code. I was updating some Express 2 to Express 3 code, and had this...

另一个可能出现这种错误的地方是同一个节点代码中的两个HTTP服务器。我更新了一些快递2以快递3码,有了这个…

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});        

// tons of shit.

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});                                                                   

And, it triggered this error.

它触发了这个错误。

#7


13  

pkill node before running your script should do the job.

在运行脚本之前,pkill节点应该完成这个任务。

#8


11  

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

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

sudo lsof -t -i tcp:8080 | xargs kill -9

#9


5  

EADDRINUSE means that the port(which we try to listen in node application) is already being used. In order to overcome, we need to identify which process is running with that port.

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

For example if we are trying to listen our node application in 3000 port. We need to check whether that port is already is being used by any other process.

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

step1:

$sudo netstat -plunt |grep :3000

That the above command gives below result.

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

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

step2:

Now you got process ID(25315), Kill that process.

现在你有了进程ID(25315),终止进程。

kill -9 25315

step3:

npm run start

Note: This solution for linux users.

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

#10


3  

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..

但这只适用于开发机器,而不是服务器控制台。

#11


3  

This error comes when you have any process running on a port on which you want to run your application.

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

how to get which process running on that port=> command: sudo netstat -ap | grep :3000

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

output : you will get the process information which is using that port

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

tcp 0 0 IPaddress:3000 : LISTEN 26869/node

IPaddress:3000:监听26869/节点

Now you can kill that process sudo kill -9 26869

现在你可以杀死这个过程sudo杀死- 926869

#12


3  

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.

错误:listen EADDRINUSE表示要分配/绑定到应用服务器的端口已经在使用。您可以为应用程序分配另一个端口。

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

获取进程id之后,执行

kill process_id

#13


2  

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.

我以前在http中见过这个错误(在节点中)。客户端,我记得,这个问题与没有初始化httpClient或在httpClient创建和/或url请求中设置错误选项有关。

#14


1  

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

在Debian中,我发现要在端口80上运行,您需要以root身份发出命令,即

sudo node app.js

I hope it helps

我希望这有助于

#15


1  

While killing the NODE_PORT, it might kill your chrome process or anything that is listening to the same port, and that's annoying.

当杀死NODE_PORT时,它可能会杀死您的chrome进程或任何正在听相同端口的东西,这很烦人。

This shell script may be helpful - in my case the port is 1337 but you can change it anytime

这个shell脚本可能会有帮助——在我的例子中,端口是1337,但是您可以随时更改它

# LOGIC

CHROME_PIDS=`pidof chrome`
PORT_PIDS=`lsof -t -i tcp:1337`

for pid in $PORT_PIDS
do

if [[ ${CHROME_PIDS} != *$pid* ]];then

    # NOT FOUND IN CHROME PIDS

    echo "Killing $pid..."
    ps -p "$pid"

    kill -kill "$pid"
    fi

done

sails lift
# OR 'node app' OR whatever that starts your node

exit

#16


1  

In my case I use a web hosting but it´s the same in local host, I used:

在我的例子中,我使用一个虚拟主机,但´s在本地主机,我使用:

ps -aef | grep 'node' 

for watch the node process then, the console shows the process with PID. for kill the process you have to use this command:

要查看节点进程,控制台将使用PID显示进程。要杀死进程,您必须使用以下命令:

kill -9 PID

where PID is the process id from the command above.

其中PID是来自上面命令的进程id。

#17


1  

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

两个服务器不能在同一个端口上监听,所以请检查其他服务器是否监听同一个端口,如果浏览器同步在同一个端口上运行,也请检查浏览器是否同步

#18


1  

For other people on windows 10 with node as localhost and running on a port like 3500, not 80 ...

对于windows 10上的其他人来说,node是localhost,运行在3500端口上,而不是80端口上……

What does not work:

什么不工作:

killall    ?  command not found
ps -aux | grep 'node'     ?     ps:  user x unknown 

What shows information but still not does work:

显示信息但仍然无效的:

 ps -aef | grep 'node'
 ps ax
 kill -9 61864

What does work:

什么工作:

Git Bash or Powershell on Windows

Windows上的Git Bash或Powershell

  net -a -o | grep 3500   (whatever port you are looking for) 

Notice the PID ( far right )
I could not get killall to work... so

注意PID(极右)我不能让killall工作…所以

  1. Open your task manager
  2. 打开你的任务管理器
  3. On processes tab , right click on Name or any column and select to include PID
  4. 在process选项卡上,右键单击名称或任何列,并选择包含PID
  5. Sort by PID, then right click on right PID and click end task.
  6. 按PID排序,然后右击PID,点击结束任务。

Now after that not so fun exercise on windows, I realized I can use task manager and find the Node engine and just end it.

现在在windows上做了这个不那么有趣的练习之后,我意识到我可以使用task manager找到节点引擎,然后结束它。

FYI , I was using Visual Studio Code to run Node on port 3500, and I use Git Bash shell inside VS code. I had exited gracefully with Ctrl + C , but sometimes this does not kill it. I don't want to change my port or reboot so this worked. Hopefully it helps others. Otherwise it is documentation for myself.

顺便提一下,我在端口3500上使用Visual Studio代码运行节点,在VS代码中使用Git Bash shell。我已经优雅地用Ctrl + C退出了,但有时这并不能抹杀它。我不想改变我的端口或重新启动,这样就可以了。希望它能帮助他人。否则就是我自己的文件。

#19


1  

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

#20


1  

In my case Apache HTTP Server was run on port 80 I solved it by issue the command as root

在我的案例中,Apache HTTP服务器是在端口80上运行的,我通过将命令作为根来解决它。

sudo killall httpd

sudo killall httpd

Update

更新

If Jenkin is installed and running on your Mac;

如果在你的Mac上安装和运行Jenkin;

  1. You can check it with sudo lsof -i tcp:8080
  2. 您可以使用sudo lsof -i tcp:8080检查它
  3. If Yes, and You want to stop Jenkins only once, run: sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
  4. 如果是,并且您只希望停止Jenkins一次,那么运行:sudo launchctl卸载/Library/LaunchDaemons/org.jenkins-ci.plist

#21


1  

I had this issue and resolved by changing the port number (select free port)

我解决了这个问题,通过更改端口号(选择free port)来解决

Windows

窗户

set PORT=5000

Mac

Mac

export PORT=5000

#22


0  

if you want to solve this

如果你想解决这个问题。

$ node server events.js:141 throw er; // Unhandled 'error' event ^

美元节点服务器事件。js:141 er扔;^ / /未处理的“错误”事件

Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Server.listen (net.js:1382:5) at EventEmitter.listen (C:\sendbox\mean\node_modules\express\lib\application .js:617:24) at Object. (C:\sendbox\mean\server.js:28:5) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32)

错误:听EADDRINUSE:::3000 at objecter .导出。_errnoException(util.js:907:11)出口。_exceptionWithHostPort(util.js:930:20)服务器。_listen2 (net.js:1250:14)在listen (net.js:1286:10)。听(EventEmitter net.js:1382:5)。听(C:\sendbox\mean\node_modules\express\lib\application .js:617:24)。(C:\ sendbox \ \ server.js:上28:5)模块。_compile(module.js:409:26)Object.Module._extensions . .js(module.js:416:10)模块。负载(module.js 343:32):

change your port number to 8000

将端口号更改为8000

#23


0  

Windows is always tricky with open source..

对于开放源码,Windows总是很棘手。

change the port simply it works

只需更改端口即可

node-inspector --web-port=8099

#24


0  

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

对于windows用户,在PowerShell窗口中执行以下命令,以杀死所有节点进程。

Stop-Process -processname node

#25


0  

The error EADDRINUSE (Address already in use) reports that there is already another process on the local system occupying that address / port.

错误EADDRINUSE(地址已经在使用)报告本地系统中已经有另一个进程占用该地址/端口。

There is a npm package called find-process which helps finding (and closing) the occupying process.

有一个名为find-process的npm包,它可以帮助查找(并关闭)占用进程。

Here is a little demo code:

这里有一个小演示代码:

const find = require('find-process')

const PORT = 80

find('port', PORT)
.then((list) => {
  console.log(`Port "${PORT}" is blocked. Killing blocking applications...`)
  const processIds = list.map((item) => item.pid)
  processIds.forEach((pid) => process.kill(pid, 10))
})

I prepared a small sample which can reproduce the EADDRINUSE error. If you launch the following program in two separate terminals, you will see that the first terminal will start a server (on port "3000") and the second terminal will close the already running server (because it blocks the execution of the second terminal, EADDRINUSE):

我准备了一个小样本,可以重现EADDRINUSE的误差。如果您在两个独立的终端上启动以下程序,您将看到第一个终端将启动一个服务器(在端口“3000”上),第二个终端将关闭已经运行的服务器(因为它阻止第二个终端EADDRINUSE的执行):

Minimal Working Example:

最小的工作示例:

const find = require('find-process')
const http = require('http')

const PORT = 3000

// Handling exceptions
process.on('uncaughtException', (error) => {
  if (error.code === 'EADDRINUSE') {
    find('port', PORT)
      .then((list) => {
        const blockingApplication = list[0]
        if (blockingApplication) {
          console.log(`Port "${PORT}" is blocked by "${blockingApplication.name}".`)
          console.log('Shutting down blocking application...')
          process.kill(blockingApplication.pid)
          // TODO: Restart server
        }
      })
  }
})

// Starting server
const server = http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.write('Hello World!')
  response.end()
})

server.listen(PORT, () => console.log(`Server running on port "${PORT}"...`))

#26


0  

I would prefer doing

我宁愿做

killall -15 node

killall -15节点

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

因为,kill -15给了进程一个清理自己的机会。现在,你可以通过

ps aux | grep node

pb0 grep节点。

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

注意:如果您不给进程一个机会来完成当前正在做的事情和清理,它可能会导致文件损坏。

#27


0  

EADDRINUSE translates to "The port you are trying to issue app.listen() on is being used by other programs". You can use a script like this to check if your port is in use and then change the port in your app.listen().

EADDRINUSE的意思是“正在尝试发出应用程序的端口。listen() on正在被其他程序使用”。您可以使用这样的脚本检查您的端口是否在使用中,然后更改app.listen()中的端口。

var net = require('net');
var errors = ['EADDRINUSE'];    
var isUsed = function(port) {
    var tester = net.createServer()
        .once('error', function (err) {
          if (!errors.includes(err.code)) {
           console.log("Port is in use, change the port.");
          }
        })
        .once('listening', function() {
            tester.once('close', function() { 
                console.log("You are good to go."); 
            })
            .close()
        })
        .listen(port);
}

You can add other errors in the errors array to check for all sorts of error types as well.

您还可以在错误数组中添加其他错误,以检查所有类型的错误类型。

#28


-2  

In my case rebooting ubuntu was helpfull.

在我的例子中,重新启动ubuntu是有帮助的。

#1


307  

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

EADDRINUSE意味着listen()试图将服务器绑定到的端口号已经在使用中。

So, in your case, there must be running a server on port 80 already.

因此,在您的示例中,必须已经在端口80上运行服务器。

If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

如果在这个端口上运行另一个webserver,则必须放置节点。服务器后面的js并通过它代理它。

You should check for the listening event like this, to see if the server is really listening:

您应该检查一下像这样的监听事件,看看服务器是否真的在监听:

var http=require('http');

var server=http.createServer(function(req,res){
    res.end('test');
});

server.on('listening',function(){
    console.log('ok, server is running');
});

server.listen(80);

#2


372  

What really helped for me was:

真正对我有帮助的是:

killall -9 node

But this will kill a system process.

但这将扼杀系统进程。

With

ps ax

you can check if it worked.

你可以检查一下是否有效。

#3


185  

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.

前面提到的killall -9节点,由Patrick建议可以按照预期工作并解决问题,但是您可能希望阅读本文的编辑部分,了解为什么kill -9可能不是最好的方法。

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):

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

lsof -i tcp:8888

lsof - 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):

然后就去做(ps -实际上不做。请继续阅读下文):

kill -9 57385

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.

编辑:我今天阅读了一个相当相关的话题,偶然发现了一个有趣的话题:为什么我不应该杀死-9个进程。

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.

通常,您应该在kill -9之前使用kill -15,以给目标进程一个自己清理的机会。(进程不能捕获或忽略SIGKILL,但它们可以而且经常捕获SIGTERM。)如果您不给流程一个机会来完成它正在做的事情并进行清理,它可能会在重新启动后留下损坏的文件(或其他状态),使其无法理解。

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

因此,如前所述,您最好以以下方式终止上述过程:

kill -15 57385

杀-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.

编辑2:正如这里的一个评论所指出的,这个错误很多时候是由于没有优雅地退出进程而导致的。这意味着,很多人使用CTRL+Z退出节点命令(或其他命令)。停止运行进程的正确方法是发出执行干净退出的CTRL+C命令。

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.

以正确的方式退出进程将释放该端口,同时关闭该端口。这将允许您重新启动流程,而不必在重新运行之前自己将其杀死。

#4


52  

** BEFORE DOWNVOTING - Please READ the answer. IT IS RELEVANT! If you are going to downvote this, leave a comment why you think it isn't relevant.

**在投反对票前-请阅读答案。这是有关!如果你打算投反对票,请留下评论,说明为什么你认为这无关紧要。


Just a head's up, Skype will sometimes listen on port 80 and therefore cause this error if you try to listen on port 80 from Node.js or any other app.

如果你试着从Node监听端口80,Skype有时会在端口80上监听,因此会导致这个错误。js或其他应用。

You can turn off that behaviour in Skype by accessing the options and clicking Advanced -> Connection -> Use port 80 (Untick this)

您可以通过访问选项并单击Advanced ->连接->使用端口80来关闭Skype中的这种行为(取消此选项)

如何修正错误:使用nodejs时听EADDRINUSE ?

#5


29  

You should try killing the process that is listening on port 80.

您应该尝试终止监听端口80的进程。

Killall will kill all the node apps running. You might not want to do that. With this command you can kill only the one app that is listening on a known port.

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

If using unix try this command:

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

sudo fuser -k 80/tcp    

#6


13  

Another thing that can give this error, is two HTTP servers in the same node code. I was updating some Express 2 to Express 3 code, and had this...

另一个可能出现这种错误的地方是同一个节点代码中的两个HTTP服务器。我更新了一些快递2以快递3码,有了这个…

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});        

// tons of shit.

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});                                                                   

And, it triggered this error.

它触发了这个错误。

#7


13  

pkill node before running your script should do the job.

在运行脚本之前,pkill节点应该完成这个任务。

#8


11  

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

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

sudo lsof -t -i tcp:8080 | xargs kill -9

#9


5  

EADDRINUSE means that the port(which we try to listen in node application) is already being used. In order to overcome, we need to identify which process is running with that port.

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

For example if we are trying to listen our node application in 3000 port. We need to check whether that port is already is being used by any other process.

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

step1:

$sudo netstat -plunt |grep :3000

That the above command gives below result.

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

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

step2:

Now you got process ID(25315), Kill that process.

现在你有了进程ID(25315),终止进程。

kill -9 25315

step3:

npm run start

Note: This solution for linux users.

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

#10


3  

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..

但这只适用于开发机器,而不是服务器控制台。

#11


3  

This error comes when you have any process running on a port on which you want to run your application.

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

how to get which process running on that port=> command: sudo netstat -ap | grep :3000

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

output : you will get the process information which is using that port

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

tcp 0 0 IPaddress:3000 : LISTEN 26869/node

IPaddress:3000:监听26869/节点

Now you can kill that process sudo kill -9 26869

现在你可以杀死这个过程sudo杀死- 926869

#12


3  

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.

错误:listen EADDRINUSE表示要分配/绑定到应用服务器的端口已经在使用。您可以为应用程序分配另一个端口。

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

获取进程id之后,执行

kill process_id

#13


2  

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.

我以前在http中见过这个错误(在节点中)。客户端,我记得,这个问题与没有初始化httpClient或在httpClient创建和/或url请求中设置错误选项有关。

#14


1  

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

在Debian中,我发现要在端口80上运行,您需要以root身份发出命令,即

sudo node app.js

I hope it helps

我希望这有助于

#15


1  

While killing the NODE_PORT, it might kill your chrome process or anything that is listening to the same port, and that's annoying.

当杀死NODE_PORT时,它可能会杀死您的chrome进程或任何正在听相同端口的东西,这很烦人。

This shell script may be helpful - in my case the port is 1337 but you can change it anytime

这个shell脚本可能会有帮助——在我的例子中,端口是1337,但是您可以随时更改它

# LOGIC

CHROME_PIDS=`pidof chrome`
PORT_PIDS=`lsof -t -i tcp:1337`

for pid in $PORT_PIDS
do

if [[ ${CHROME_PIDS} != *$pid* ]];then

    # NOT FOUND IN CHROME PIDS

    echo "Killing $pid..."
    ps -p "$pid"

    kill -kill "$pid"
    fi

done

sails lift
# OR 'node app' OR whatever that starts your node

exit

#16


1  

In my case I use a web hosting but it´s the same in local host, I used:

在我的例子中,我使用一个虚拟主机,但´s在本地主机,我使用:

ps -aef | grep 'node' 

for watch the node process then, the console shows the process with PID. for kill the process you have to use this command:

要查看节点进程,控制台将使用PID显示进程。要杀死进程,您必须使用以下命令:

kill -9 PID

where PID is the process id from the command above.

其中PID是来自上面命令的进程id。

#17


1  

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

两个服务器不能在同一个端口上监听,所以请检查其他服务器是否监听同一个端口,如果浏览器同步在同一个端口上运行,也请检查浏览器是否同步

#18


1  

For other people on windows 10 with node as localhost and running on a port like 3500, not 80 ...

对于windows 10上的其他人来说,node是localhost,运行在3500端口上,而不是80端口上……

What does not work:

什么不工作:

killall    ?  command not found
ps -aux | grep 'node'     ?     ps:  user x unknown 

What shows information but still not does work:

显示信息但仍然无效的:

 ps -aef | grep 'node'
 ps ax
 kill -9 61864

What does work:

什么工作:

Git Bash or Powershell on Windows

Windows上的Git Bash或Powershell

  net -a -o | grep 3500   (whatever port you are looking for) 

Notice the PID ( far right )
I could not get killall to work... so

注意PID(极右)我不能让killall工作…所以

  1. Open your task manager
  2. 打开你的任务管理器
  3. On processes tab , right click on Name or any column and select to include PID
  4. 在process选项卡上,右键单击名称或任何列,并选择包含PID
  5. Sort by PID, then right click on right PID and click end task.
  6. 按PID排序,然后右击PID,点击结束任务。

Now after that not so fun exercise on windows, I realized I can use task manager and find the Node engine and just end it.

现在在windows上做了这个不那么有趣的练习之后,我意识到我可以使用task manager找到节点引擎,然后结束它。

FYI , I was using Visual Studio Code to run Node on port 3500, and I use Git Bash shell inside VS code. I had exited gracefully with Ctrl + C , but sometimes this does not kill it. I don't want to change my port or reboot so this worked. Hopefully it helps others. Otherwise it is documentation for myself.

顺便提一下,我在端口3500上使用Visual Studio代码运行节点,在VS代码中使用Git Bash shell。我已经优雅地用Ctrl + C退出了,但有时这并不能抹杀它。我不想改变我的端口或重新启动,这样就可以了。希望它能帮助他人。否则就是我自己的文件。

#19


1  

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

#20


1  

In my case Apache HTTP Server was run on port 80 I solved it by issue the command as root

在我的案例中,Apache HTTP服务器是在端口80上运行的,我通过将命令作为根来解决它。

sudo killall httpd

sudo killall httpd

Update

更新

If Jenkin is installed and running on your Mac;

如果在你的Mac上安装和运行Jenkin;

  1. You can check it with sudo lsof -i tcp:8080
  2. 您可以使用sudo lsof -i tcp:8080检查它
  3. If Yes, and You want to stop Jenkins only once, run: sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
  4. 如果是,并且您只希望停止Jenkins一次,那么运行:sudo launchctl卸载/Library/LaunchDaemons/org.jenkins-ci.plist

#21


1  

I had this issue and resolved by changing the port number (select free port)

我解决了这个问题,通过更改端口号(选择free port)来解决

Windows

窗户

set PORT=5000

Mac

Mac

export PORT=5000

#22


0  

if you want to solve this

如果你想解决这个问题。

$ node server events.js:141 throw er; // Unhandled 'error' event ^

美元节点服务器事件。js:141 er扔;^ / /未处理的“错误”事件

Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Server.listen (net.js:1382:5) at EventEmitter.listen (C:\sendbox\mean\node_modules\express\lib\application .js:617:24) at Object. (C:\sendbox\mean\server.js:28:5) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32)

错误:听EADDRINUSE:::3000 at objecter .导出。_errnoException(util.js:907:11)出口。_exceptionWithHostPort(util.js:930:20)服务器。_listen2 (net.js:1250:14)在listen (net.js:1286:10)。听(EventEmitter net.js:1382:5)。听(C:\sendbox\mean\node_modules\express\lib\application .js:617:24)。(C:\ sendbox \ \ server.js:上28:5)模块。_compile(module.js:409:26)Object.Module._extensions . .js(module.js:416:10)模块。负载(module.js 343:32):

change your port number to 8000

将端口号更改为8000

#23


0  

Windows is always tricky with open source..

对于开放源码,Windows总是很棘手。

change the port simply it works

只需更改端口即可

node-inspector --web-port=8099

#24


0  

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

对于windows用户,在PowerShell窗口中执行以下命令,以杀死所有节点进程。

Stop-Process -processname node

#25


0  

The error EADDRINUSE (Address already in use) reports that there is already another process on the local system occupying that address / port.

错误EADDRINUSE(地址已经在使用)报告本地系统中已经有另一个进程占用该地址/端口。

There is a npm package called find-process which helps finding (and closing) the occupying process.

有一个名为find-process的npm包,它可以帮助查找(并关闭)占用进程。

Here is a little demo code:

这里有一个小演示代码:

const find = require('find-process')

const PORT = 80

find('port', PORT)
.then((list) => {
  console.log(`Port "${PORT}" is blocked. Killing blocking applications...`)
  const processIds = list.map((item) => item.pid)
  processIds.forEach((pid) => process.kill(pid, 10))
})

I prepared a small sample which can reproduce the EADDRINUSE error. If you launch the following program in two separate terminals, you will see that the first terminal will start a server (on port "3000") and the second terminal will close the already running server (because it blocks the execution of the second terminal, EADDRINUSE):

我准备了一个小样本,可以重现EADDRINUSE的误差。如果您在两个独立的终端上启动以下程序,您将看到第一个终端将启动一个服务器(在端口“3000”上),第二个终端将关闭已经运行的服务器(因为它阻止第二个终端EADDRINUSE的执行):

Minimal Working Example:

最小的工作示例:

const find = require('find-process')
const http = require('http')

const PORT = 3000

// Handling exceptions
process.on('uncaughtException', (error) => {
  if (error.code === 'EADDRINUSE') {
    find('port', PORT)
      .then((list) => {
        const blockingApplication = list[0]
        if (blockingApplication) {
          console.log(`Port "${PORT}" is blocked by "${blockingApplication.name}".`)
          console.log('Shutting down blocking application...')
          process.kill(blockingApplication.pid)
          // TODO: Restart server
        }
      })
  }
})

// Starting server
const server = http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.write('Hello World!')
  response.end()
})

server.listen(PORT, () => console.log(`Server running on port "${PORT}"...`))

#26


0  

I would prefer doing

我宁愿做

killall -15 node

killall -15节点

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

因为,kill -15给了进程一个清理自己的机会。现在,你可以通过

ps aux | grep node

pb0 grep节点。

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

注意:如果您不给进程一个机会来完成当前正在做的事情和清理,它可能会导致文件损坏。

#27


0  

EADDRINUSE translates to "The port you are trying to issue app.listen() on is being used by other programs". You can use a script like this to check if your port is in use and then change the port in your app.listen().

EADDRINUSE的意思是“正在尝试发出应用程序的端口。listen() on正在被其他程序使用”。您可以使用这样的脚本检查您的端口是否在使用中,然后更改app.listen()中的端口。

var net = require('net');
var errors = ['EADDRINUSE'];    
var isUsed = function(port) {
    var tester = net.createServer()
        .once('error', function (err) {
          if (!errors.includes(err.code)) {
           console.log("Port is in use, change the port.");
          }
        })
        .once('listening', function() {
            tester.once('close', function() { 
                console.log("You are good to go."); 
            })
            .close()
        })
        .listen(port);
}

You can add other errors in the errors array to check for all sorts of error types as well.

您还可以在错误数组中添加其他错误,以检查所有类型的错误类型。

#28


-2  

In my case rebooting ubuntu was helpfull.

在我的例子中,重新启动ubuntu是有帮助的。