我已经在一个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来检查哪个进程正在使用该端口,但是没有使用该端口。我也尝试重新启动我的笔记本电脑,但我仍然得到同样的错误。


当前回答

您可以搜索如何终止该进程。

对于Linux/Mac OS搜索(sudo)在终端中运行:

$ lsof -i tcp:3000
$ kill -9 PID

在Windows上:

netstat -ano | findstr :3000
tskill typeyourPIDhere 

在git bash中将tskill更改为taskkill

其他回答

有时会发生,就像@sova提议的那样,有时会发生在我身上,使用EADDR。通常会有一个终端窗口隐藏在后台,它仍在运行应用程序。我也是这样。

它发生了,当你打开终端很长一段时间,是的,你有权利,你必须停止这个过程。但有时它并没有在背景中停止。因此,最好的解决方案是关闭终端,然后重新启动。它会解决你的问题。因为这对我来说很管用。

同时,

sudo lsof -i:<PORT_NO>

当前关闭实例,但无法在后台停止进程。所以有一次,

sudo kill <PID>

工作,但再次当我们更新我们的代码和保存,这个问题再次发生与Nodemon。

所以退出终端就能解决问题。或

  killall -9 node

Try opening the localhost in your browser. Just type: localhost:3000 in the address bar. If the app opens-up, it means your previous npm run is still active. Now, you can just make changes to the code and see the effects if you are designing the same app, or if you wanna run another app, just tweak the code (in index.js of previously running app) a little-bit and (probably refresh the browser tab) to make it crash ;)..... Now go run npm run start again from your new app directory. Hope this helps! :)

or

你可以打开任务管理器(WINDOWS_KEY+X >任务管理器),你会看到“Node.js:服务器端JavaScript”行。选择它并结束任务....现在应该可以工作了!!

如果不是,将应用程序的.env文件更改为包含端口:3002并运行新应用程序。这将允许您在不同的端口上运行两个独立的应用程序。干杯! !

 killall -9 node

当你在远程开发时,上面的命令可以退出vs code ssh连接,并杀死所有可能导致问题的节点进程,特别是如果你在生产中有一些应用程序使用node,有一个更好的方法,使用netstat获取所有节点进程及其所使用的端口,然后通过PID杀死你想要的唯一一个

 netstat -lntp | grep node

您将获得所有节点进程

 tcp6  0      0 :::5744    :::*    LISTEN     3864/node

然后当你得到PID(3864)时,通过PID终止进程

   kill -9 PID

or

   kill -HUP PID 

确保在hosts文件中配置了localhost

127.0.0.1       localhost

https://github.com/facebook/create-react-app/issues/2843

我在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