Javascript Node.js 端口 3000 已在使用中,但实际上并未使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39322089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Port 3000 already in use but it actually isn't?
提问by user2573690
I have been working with a node.js project for a few weeks and it has been working great. Usually, I use npm start
to run my app and view it in a browser on localhost, port 3000.
我使用 node.js 项目已经有几个星期了,而且效果很好。通常,我npm start
用来运行我的应用程序并在本地主机的浏览器中查看它,端口为 3000。
Today, I started to get the following error while using npm start:
今天,我在使用 npm start 时开始出现以下错误:
Server started on port 3000
Port 3000 is already in use
I have checked the resource monitor and I have no other process running on port 3000. Why would I be getting this error message?
我已经检查了资源监视器并且我没有在端口 3000 上运行其他进程。为什么我会收到此错误消息?
In my app.js I have the following code to set the port...is this incorrect? It worked fine before so I'm not sure what I am doing wrong.
在我的 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'));
});
Thanks for the help!
谢谢您的帮助!
EDIT:
编辑:
I have tried running netstat and TCPView to check what process is using the port, but there is nothing using that port. I also tried restarting my laptop but I still get the same error.
我尝试运行 netstat 和 TCPView 来检查哪个进程正在使用该端口,但没有使用该端口。我也尝试重新启动我的笔记本电脑,但我仍然遇到同样的错误。
回答by Animesh Singh
You can search on how to kill that process.
您可以搜索如何终止该进程。
For Linux/MacOS search (sudo) run
this in the terminal:
对于Linux/MacOS (sudo) run
,在终端中搜索:
$ lsof -i tcp:3000
$ kill -9 PID
On Windows:
在 Windows 上:
netstat -ano | findstr :3000
tskill typeyourPIDhere
change tskill
for taskkill
in git bash
更改tskill
为taskkill
Git中的bash
回答by techyaura
Sometimes it happens, as @sova proposed This happens to me sometimes, EADDR in use. Typically there is a terminal window hiding out in the background that is still running the app.And that's also right with me.
有时它会发生,正如@sova 建议的那样有时会发生在我身上,正在使用 EADDR。通常有一个隐藏在后台的终端窗口仍在运行该应用程序。这对我来说也是正确的。
It happens, when you have opened terminal for long time, yeah you have right, you have stop the process. But sometimes it didn't stop in the background. So best solution is that you close the terminal and start it again.It will solves your problem. becuase in my case it works.
它发生了,当你打开终端很长时间时,是的,你是对的,你已经停止了这个过程。但有时它并没有在后台停止。所以最好的解决方案是关闭终端并重新启动它。它会解决你的问题。因为在我的情况下它有效。
Also,
还,
sudo lsof -i:<PORT_NO>
close the instance for present time but unable to stop the process in background. So for one time,
关闭当前实例,但无法在后台停止进程。所以有一次,
sudo kill <PID>
works, but again when we update our code and save, this problem occurs again as with Nodemon.
有效,但是当我们更新我们的代码并保存时,这个问题再次发生,就像Nodemon 一样。
So exit the terminal will solve the problem.OR
所以退出终端即可解决问题。或者
killall -9 node
回答by PRAKASH THOMAS VARGHESE
For windows, The Task Manager would definitely show a node process running. Try to kill the process, it will solve the problem.
对于 Windows,任务管理器肯定会显示正在运行的节点进程。尝试杀死进程,它会解决问题。
回答by Mahyar
I had the same problem. (The below steps work fine on Windows 10):
我有同样的问题。(以下步骤在 Windows 10 上运行良好):
- Open Task manager (press Ctrl+Alt+Delete)
- Select the 'Processes tab'
- Search for 'Node.js: Server-side JavaScript'
- Select it and click on 'End task' button
- 打开任务管理器(按Ctrl+ Alt+ Delete)
- 选择“进程选项卡”
- 搜索“Node.js:服务器端 JavaScript”
- 选择它并单击“结束任务”按钮
Now you can run npm start
.
现在您可以运行npm start
.
Hope it helps you.
希望对你有帮助。
回答by James Smith
I've seen the same thing and tried all the suggestions above without success. Here are steps that resolve it for me: - turn off wifi - npm start (this should work) - turn on wifi
我见过同样的事情,并尝试了上面的所有建议,但都没有成功。以下是为我解决问题的步骤: - 关闭 wifi - npm start(这应该可以工作) - 打开 wifi
I'm not exactly sure what the root issue is but that resolved it for me.
我不确定根本问题是什么,但这为我解决了。
回答by Afeesudheen
Killing a process that owns port 3000
杀死一个拥有 3000 端口的进程
First, let's take a look at how we can kill a process that has a port open.
首先,让我们看看如何杀死一个打开了端口的进程。
Using the lsof command, we can retrieve the PID that has the given port:
使用 lsof 命令,我们可以检索具有给定端口的 PID:
$ lsof -i :3000 -t
12345
Then we can kill this process just by doing:
然后我们可以通过执行以下操作来终止这个进程:
$ kill 12345
Let's turn this into a one-liner:
让我们把它变成一个单行:
lsof -i 3000 -t | xargs kill
If you're using an environment variable to set the server port, we can specify that instead of hardcoding our values:
如果您使用环境变量来设置服务器端口,我们可以指定它而不是硬编码我们的值:
lsof -i ${PORT} -t | xargs kill
Lastly, we can default to port 3000 if the environment variable isn't set:
最后,如果未设置环境变量,我们可以默认使用端口 3000:
lsof -i ${PORT:-3000} -t | xargs kill
Getting nodemon to execute hooks
让 nodemon 执行钩子
Nodemon lets you set up event hooks through nodemon.json configuration file:
Nodemon 允许您通过 nodemon.json 配置文件设置事件挂钩:
{
"events": {
"crash": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'"
}
}
This will cause nodemon to execute sh -c 'lsof -i :${PORT:-3000} -t | xargs
kill command whenever your app crashes, thereby killing the child process it spawned that's keeping the port open.
这将导致 nodemon${PORT:-3000} -t | xargs
在您的应用程序崩溃时执行 sh -c 'lsof -i : kill 命令,从而杀死它产生的保持端口打开的子进程。
or you can try this one
或者你可以试试这个
fuser -k PORT-NO/tcp
eg:
例如:
fuser -k 3000/tcp
回答by aygunyilmaz
I was using express server with nodemonon NodeJS. I got the following message and it seems an error:
我在NodeJS上使用带有nodemon 的快速服务器。我收到以下消息,似乎是一个错误:
$ node ./bin/www
Port 3000 is already in use
There is a general solution that if you terminate all node server connections, you can add this code in your package.json file:
有一个通用的解决方案,如果您终止所有节点服务器连接,您可以在 package.json 文件中添加以下代码:
"scripts": {
"start": "node ./bin/www",
"stop": "taskkill -f -im node.exe"
},
In addition, I've found several solutions windows command and bash on Win 10 x64.
此外,我在 Win 10 x64 上找到了几个解决方案 windows command 和 bash。
All my notes are here:
我所有的笔记都在这里:
# Terminate all NodeJS Server Connections
# 终止所有 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.
# Example: Open the Windows Task Manager and see "node.exe" PID number on Windows
# 示例:打开 Windows 任务管理器并查看 Windows 上的“node.exe”PID 编号
>> Command Line
$ netstat /?
$ netstat -a -n -o
$ netstat -ano
# Kill a process in Windows by Port Number (Example)
# 通过端口号杀死 Windows 中的进程(示例)
For Help:
求助:
$ taskkill /?
$ tskill /?
Code 1:
代码 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).
Code 2:
代码 2:
$ taskkill -f -pid 14228
SUCCESS: The process with PID 14228 has been terminated.
Code 3:
代码 3:
$ tskill 14228
# Command line for looking at specific port
# 查看特定端口的命令行
in cmd:
在 cmd 中:
$ netstat -ano | find "14228"
in bash:
在 bash 中:
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
# Find node.exe using "tasklist" command
# 使用“tasklist”命令查找node.exe
in cmd:
在 cmd 中:
$ tasklist | find "node"
in bash:
在 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
回答by Penny Liu
回答by Omar bakhsh
Open Task Manager (press Ctrl+Alt+Del Select the 'Processes Tab' Search for 'Node.js: Server-side JavaScript' Select it and click on 'End task' button
打开任务管理器(按 Ctrl+Alt+Del 选择“进程选项卡”搜索“Node.js:服务器端 JavaScript”选择它并单击“结束任务”按钮
回答by sova
This happens to me sometimes, EADDR in use. Typically there is a terminal window hiding out in the background that is still running the app. You can stop process with ctrl+C in the terminal window.
这有时会发生在我身上,正在使用 EADDR。通常有一个隐藏在后台的终端窗口仍在运行应用程序。您可以在终端窗口中使用 ctrl+C 停止进程。
Or, perhaps you are listening to the port multiple times due to copy/pasta =)
或者,您可能由于复制/意大利面而多次监听端口 =)