如何解决:-监听EADDRINUSE:地址已在使用中-

huangapple go评论88阅读模式
英文:

how to solve:-listen EADDRINUSE: address already in use-

问题

以下是您要翻译的部分:

"As I am trying to run the webservice shown as follows:

const port = 3000;
app.get('/hello', (req, res, next) => {
	res.send('Hello, World!');
});
app.listen(port, () => {
	console.log('listening to port', port);
}); 

For the very first time, it works fine. But since then, when I run node index.js which contains the webservice mentioned above, I receive the error posted below.

To solve this issue I ran the following command:

npx kill-port 3000

but that did not solve the problem.

Please let me know if there is another way to solve this issue.

Note: I tried to use several other ports numbers, but I receive the same error message with the new port number indicated in the error message.

Error:

node:events:489
  throw er; // Unhandled 'error' event
  ^
Error: listen EADDRINUSE: address already in use :::3000"
英文:

As I am trying to run the webservice shown as follows:

const port = 3000;
app.get('/hello', (req, res, next) => {
	res.send('Hello, World!');
});
app.listen(port, () => {
	console.log('listening to port', port);
}); 

For the very first time, it works fine. But since then, when I run node index.js which contains the webservice mentioned above, I receive the error posted below.

To solve this issue I ran the following command:

npx kill-port 3000

but that did not solve the problem.

Please let me know if there is another way to solve this issue.

Note: I tried to use several other ports numbers, but I receive the same error message with the new port number indicated in the error message.

Error:

node:events:489
  throw er; // Unhandled 'error' event
  ^
Error: listen EADDRINUSE: address already in use :::3000

答案1

得分: 1

看起来端口3000已被另一个进程使用。

如果你使用Linux作为操作系统:

  • 要查看正在监听的端口列表
ss -ntlp
# 或者
netstat -ntlp
  • 要特定检查端口3000
netstat -ntlp | grep ':3000'

接下来,终止使用该端口的进程(首先要检查它是什么)。

kill <PID>
kill -9 <PID>  # 如果kill命令不起作用

如果你使用Windows或macOS,也有相应的方法来列出正在监听的端口并终止使用所需端口的进程。

Windows

netstat -ano | findstr :3000
taskkill /PID <PID> /F

macOS

lsof -ti:3000
kill <PID>
kill -9 <PID>  # 如果kill命令不起作用
英文:

It seems that port 3000 is already in use by another process.

If you use Linux as OS:

  • For a list of listening ports
ss -ntlp
# or
netstat -ntlp
  • To check specifically for port 3000
netstat -ntlp | grep &#39;:3000&#39;

Next, kill the process using the port (check what it is first).

kill &lt;PID&gt;
kill -9 &lt;PID&gt;  # if kill does not work

If you're using Windows or macOS, there's an equivalent way to list listening ports and kill the process using the desired port.

Windows :

netstat -ano | findstr :3000
taskkill /PID &lt;PID&gt; /F

MacOS:

lsof -ti:3000
kill &lt;PID&gt;
kill -9 &lt;PID&gt; # if kill does not work

答案2

得分: 1

从任务管理器中结束该进程。

结束任务

英文:

Stop the process from Task Manager.

end the task

答案3

得分: 0

正如您提到的,第一次运行正常,然后第二次开始出错。

这与您停止应用程序的方式有关,如果您使用Ctrl + Z(或在Mac上使用cmd + Z),应用程序将在终端上停止,但进程不会被终止。

正确的方式是Ctrl + C(或在Mac上使用cmd + C),它将发送SIGINT信号,将终止应用程序。

英文:

As you mentioned, the very first time is works fine & then it throws an error second time onwards.

It has to do with the way you are stopping the application if your doing Ctrl + Z (or cmd + Z on Mac) the app will be stopped on terminal but the process won't be killed.

The correct way is Ctrl + C (or cmd + C on Mac), it will send SIGINT which will kill the application.

huangapple
  • 本文由 发表于 2023年6月22日 15:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定