[英]How to fix "Error: listen EADDRINUSE: address already in use :::5000" Unhandled 'error' event
i create a nodejs server (a loginapp) but when i try to node app a error (she was not here before) came out:我创建了一个 nodejs 服务器(一个 loginapp)但是当我尝试节点应用程序时出现错误(她之前不在这里):
events.js:167
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (net.js:1290:14)
at listenInCluster (net.js:1338:12)
at Server.listen (net.js:1425:7)
at Function.listen (C:\Users\Corentin\Documents\loginapp\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Corentin\Documents\loginapp\app.js:81:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1317:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
I think there is a link with mongo but i didnt see the error,我认为与 mongo 有联系,但我没有看到错误,
This is my app.js code (NOT FULL) ask me if you need more of my code这是我的 app.js 代码(不完整)问我是否需要我的更多代码
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local'),Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp', { useNewUrlParser: true });
var db = mongoose.connection;
var routes = require('./routes/index');
var users = require('./routes/users');
// APP INIT
var app = express();
// VIEW ENGINE
app.set('views', path.join(__dirname,'views'));
app.engine('handlebars', exphbs({ defaultLayout:'layout' }));
app.set('view engine','handlebars');
app.use('/', routes);
app.use('/users', users);
// SET PORT
app.set('port', (5000));
app.listen(app.get('port'), () => {
console.log('Server lancé sur le port ' + app.get('port'));
});
This is my users.js file这是我的 users.js 文件
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var User = require('../models/user');
// REGISTER
router.get('/register', (req, res) => {
res.render('register');
});
// LOGIN
router.get('/login', (req, res) => {
res.render('login');
});
// REGISTER
router.post('/register', (req, res) => {
var name = req.body.name;
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var password2 = req.body.password2;
// VALIDATION
req.checkBody('name','Name is required').notEmpty();
req.checkBody('username','Username is required').notEmpty();
req.checkBody('email','Email is required').notEmpty();
req.checkBody('email','Email is not valid').isEmail();
req.checkBody('password','Password is required').notEmpty();
req.checkBody('password2','Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors) {
res.render('register', {
errors:errors
});
} else {
var newUser = new User({
name: name,
username: username,
email: email,
password: password
});
User.createUser(newUser, function(err, user) {
if(err) throw err;
console.log(user);
});
req.flash('success_msg', 'You are now registered and u can log');
res.redirect('/users/login');
}
});
module.exports = router;
And the user.js file和 user.js 文件
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
// USER SCHEMA
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true
},
name: {
type: String
},
email: {
type: String
},
password: {
type: String
}
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser, callback) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
}
Thank a lot to the person who can help me !非常感谢能帮助我的人!
Try this one试试这个
sudo lsof -i :5000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME命令 PID 用户 FD 类型 设备大小/关闭节点名称
node 20152 abc 21u IPv6 195004 0t0 TCP *:http (LISTEN)节点 20152 abc 21u IPv6 195004 0t0 TCP *:http(听)
sudo kill -9 20152
这对我有用:
sudo killall -9 node
Your express server tries to open Port 500您的快递服务器尝试打开端口 500
app.set('port', (5000));
And the error message suggests, that this port is already in use.并且错误消息表明该端口已被使用。
You can either change the port of the express server or find the application that uses port 5000 and see if you can change the port there.您可以更改 express 服务器的端口,也可以找到使用端口 5000 的应用程序,然后查看是否可以更改那里的端口。
Does this help?这有帮助吗?
我发现fuser -k 5000/tcp
是杀死特定端口上的进程的最快速方法。
You should try :你应该试试 :
netstat -lpn | grep 5000
and then :进而 :
kill -9 PID
I had this error and what I did wrong was I declared the app.listen()
twice and hence the port was already in use.我有这个错误,我做错的是我声明了
app.listen()
两次,因此端口已经在使用中。 Make sure you only do it once.确保你只做一次。
你有两个终端打开,并连接到端口 5000,杀死一个!
restart your laptop/server, it will release all the busy ports, then try again...u can also use重启你的笔记本电脑/服务器,它会释放所有繁忙的端口,然后再试一次......你也可以使用
ps aux | grep node
and then kill the process using:然后使用以下命令终止进程:
kill -9 PID
but most of the times it wont work for nodemon, and didnt work for me.但大多数时候它对 nodemon 不起作用,对我也不起作用。
I know this is an old thread, but maybe someone will find this useful.我知道这是一个旧线程,但也许有人会发现这很有用。
In order to be more efficient, I added the following bash script to an alias in bashrc file, so I don't have to type lsof -i:5000
, then copy the PID, then run kill -9 PID
.为了更高效,我将以下 bash 脚本添加到 bashrc 文件中的别名中,这样我就不必键入
lsof -i:5000
,然后复制 PID,然后运行kill -9 PID
。
In the .bashrc (you can find it normally in the path ~/.bashrc ), put somewhere inside it the following:在.bashrc 中(您通常可以在~/.bashrc路径中找到它),将以下内容放入其中:
alias k5k='lsof -t -i:5000 | xargs kill -9'
I named this alias k5k for "kill 5 thousand(k)", but you name it whatever you want.我将此别名命名为k5k为“杀死 5 千(k)”,但您可以随意命名。
Then you can just type k5k
and it will kill all instances running on port 5000.然后你只需输入
k5k
,它就会杀死在端口 5000 上运行的所有实例。
this error is showing because your mongodb is running... ie mongod.显示此错误是因为您的 mongodb 正在运行...即 mongod。 so first exit from that and then restart your visual studio code.. it will definetly work.
所以首先退出,然后重新启动你的 Visual Studio 代码..它肯定会工作。
Try sudo pkill node
.试试
sudo pkill node
。 Don't forget to use sudo
permission.不要忘记使用
sudo
权限。 It's really worked for me.它真的对我有用。
I tried kill -9 PID
.我试过
kill -9 PID
。 but it reoccurs on every rs
.但它会在每个
rs
上再次发生。
Specific port running checker特定端口运行检查器
sudo netstat -lpn |grep :5000
须藤 netstat -lpn |grep :5000
tcp6 | tcp6 | 0 |
0 | 0 :::5000 |
0 ::: 5000 | :::* |
:::* | LISTEN |
听 | 6782/java
6782/java
kill 6782
杀死 6782
--------------------- OR --------------------- - - - - - - - - - - - 或者 - - - - - - - - - - -
sudo lsof -t -i:5000
须藤 lsof -t -i:5000
sudo kill -9 $(sudo lsof -t -i:5000)
须藤杀 -9 $(须藤 lsof -t -i:5000)
I just restart the terminal and run again:我只是重新启动终端并再次运行:
npm start
启动
it will work fine again它会再次正常工作
(I'm using ubuntu terminal) (我正在使用 ubuntu 终端)
on windows:在窗户上:
ctl + shift
while clicking on it - or right-click and choose Run as Administrator
)ctl + shift
单击它 - 或右键单击并选择Run as Administrator
)netstat -ano|findstr "PID :8081" // replace 8081 with your in-use port
You'll get a result like:你会得到如下结果:
// replace 1500 with your target PID
taskkill /pid 1500 /f
netstat -ano | findstr:5000
netstat -ano | findstr:5000
and after this simply, type taskkill /F /PID yourNumber(11076)
netstat -ano | findstr:5000
然后简单地输入taskkill /F /PID yourNumber(11076)
then start, server use: npm run dev
然后启动,服务器使用:
npm run dev
I just close all the windows of IDE or command prompt and restart the machine.我只是关闭 IDE 或命令提示符的所有 windows 或命令提示符并重新启动机器。 It worked fine for me.
它对我来说很好。 Hopefully, there's some random program running on that specific port.
希望有一些随机程序在该特定端口上运行。
sudo lsof -t -i tcp:portNumber | sudo lsof -t -i tcp:portNumber | xargs kill -9
xargs 杀死 -9
This will work if your portNumber is already in use with another app.js file.如果您的 portNumber 已与另一个 app.js 文件一起使用,这将起作用。
This is the Error that happens when we are using the same port number in another websites or you are trying to run the application in the terminal at the same time that application was running on another terminal .这是当我们在另一个网站上使用相同的端口号或者您试图在终端上运行该应用程序的同时该应用程序正在另一个终端上运行时发生的错误。 For me, The Situation was that I was already running the node Application in my terminal But I am trying to run it again using npm start command on the other terminal so that it shows this error.
对我来说,情况是我已经在我的终端中运行节点应用程序但我试图在另一个终端上使用 npm 启动命令再次运行它,以便它显示此错误。 Solution: You can check whether your application is running or not in the terminal or command prompt.
解决方案:您可以在终端或命令提示符中检查您的应用程序是否正在运行。 If it was running then you can quit it and run it again then it will be working fine.
如果它正在运行,那么您可以退出并再次运行它,然后它将正常工作。
If you are on a mac port 5000 will by default be used by AirPlay .如果您使用的是mac端口 5000,则默认情况下AirPlay将使用该端口。
To turn AirPlay off and thereby free up port 5000:要关闭 AirPlay 并释放端口 5000:
This has fixed the problem for me.这已经解决了我的问题。
MacBook Air 2020 MacBook 空气 2020
Apple M1苹果M1
macOS Monterey 12.6 macOS 蒙特雷 12.6
16/256GB 16/256GB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.