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 :

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,

This is my app.js code (NOT FULL) ask me if you need more of my code

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

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

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
node 20152 abc 21u IPv6 195004 0t0 TCP *:http (LISTEN)

sudo kill -9 20152

This works for me:

sudo killall -9 node

Your express server tries to open Port 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.

Does this help?