我已经在一个node.js项目上工作了几个星期,它一直工作得很好。通常,我使用npm start来运行我的应用程序,并在localhost上的浏览器中查看它,端口3000。

今天,我在使用npm start时开始得到以下错误:

Server started on port 3000                                                                                                                                                                                         
Port 3000 is already in use 

我已经检查了资源监视器,我没有在端口3000上运行其他进程。我为什么会得到这个错误消息?

在我的app.js中,我有以下代码来设置端口…这是错误的吗?它以前工作得很好,所以我不确定我做错了什么。

// Set Port
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function() {
    console.log('Server started on port '+app.get('port'));
});

谢谢你的帮助!


编辑:

我已经尝试运行netstat和TCPView来检查哪个进程正在使用该端口,但是没有使用该端口。我也尝试重新启动我的笔记本电脑,但我仍然得到同样的错误。


当前回答

我已经解决了这个问题,因为MongoDB或有另一个应用程序,你之前在这个端口上运行它, 为了解决这个问题,从任务管理器中关闭进程, 或者将端口的编号从3000更改为任何其他端口。

其他回答

通过输入命令检查在同一端口上运行的任何进程:

sudo ps -ef

您可以找到在各个节点端口上运行的进程,然后通过

kill -9 <node id>

如果问题仍然存在,那么就杀死所有节点

killall node

回复晚了,但可能会有帮助:

在我的例子中,没有任何东西使用端口3000(与OP相同,但所有的答案都是关于杀死使用该端口的进程——这没有帮助)。

然而,在任务管理器中,我们运行的两个node.exe副本,即使我杀死它们,也会重新启动。如果你在任务管理器中右键单击node.exe,你会看到这些进程在哪里运行。对我来说,原来Adobe Creative Cloud打包了自己的node.exe,这给我带来了问题。重命名文件(因为我没有使用云服务)对我来说很有用。

当命令中没有列出任何任务时,就会发生这种情况

lsof -i:3000

还是会得到一个错误

Error: listen EADDRINUSE: address already in use 0.0.0.0:3000

一个原因是在Nginx conf.d检查中,没有任何配置在端口3000上监听

我在NodeJS上使用nodemon快速服务器。 我收到以下消息,这似乎是一个错误:

$ node ./bin/www
Port 3000 is already in use

有一个通用的解决方案,如果您终止所有节点服务器连接,则可以在包中添加此代码。json文件:

"scripts": {
    "start": "node ./bin/www",
    "stop": "taskkill -f -im node.exe"
},

此外,我还找到了几个解决方案,windows命令和bash在win10 x64。

我所有的笔记都在这里:


#终止所有NodeJS服务器连接

$ taskkill -f -im node.exe
SUCCESS: The process "node.exe" with PID 14380 has been terminated.
SUCCESS: The process "node.exe" with PID 18364 has been terminated.
SUCCESS: The process "node.exe" with PID 18656 has been terminated.

#示例:打开Windows任务管理器,查看Windows上的“node.exe”PID编号

>> Command Line
$ netstat /?
$ netstat -a -n -o
$ netstat -ano

#根据端口号杀死Windows进程(示例)

寻求帮助:

$ taskkill /?
$ tskill /?

代码1:

$ taskkill -pid 14228
ERROR: The process with PID 14228 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

代码2:

$ taskkill -f -pid 14228
SUCCESS: The process with PID 14228 has been terminated.

代码3:

$ tskill 14228

#查看特定端口的命令行

在cmd:

$ netstat -ano | find "14228"

在bash中:

$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228

使用tasklist命令找到node.exe

在cmd:

$ tasklist | find "node"

在bash中:

$ tasklist | grep node
$ tasklist | grep node.exe
node.exe                     14228 Console                    2     48,156 K
node.exe                     15236 Console                    2     24,776 K
node.exe                     19364 Console                    2     24,428 K

也许你可以拿这个作参考。这个命令行可以终止在给定端口上运行的进程。

npx kill-port 3000


杀死多个端口。

npx kill-port 3000 8080 4200