[Fixed] Error: listen EADDRINUSE: address already in use :::6000

Issue

i have started my server many times like 4000 5000 6000 but getting same answer, on my port throw er; // Unhandled ‘error’ event i dont know why i have changed it many times , i also kill my server with ctrl c and then re started but get same error Error: listen EADDRINUSE: address already in use :::6000

const express = require('express');
const app = express(); 
const Joi = require('joi');
app.use(express.json());
const port = 6000;

const courses =[
    { id:1,
         name:'courses 1'},
    { id:2, name:'courses 2'},
    { id:1, name:'courses 3'},
];
app.get('/',(req,res)=>{
    res.send("hellow  request for this site has been successfully done");
});
app.get('/api/courses',(req,res)=>{
    res.send(courses);
});
app.get('/api/courses/:id',(req,res)=>{
    let course = courses.find(c=> c.id === parseInt (req.params.id));
    if (!course)res.status(404).send('<h1> 404 found</h1>')
    res.send(course);
});
app.put('/api/courses/:id',(req,res)=>{
    let course = courses.find(c=> c.id === parseInt (req.params.id));
    if (!course)res.status(404).send('<h1> 404 found</h1>')
    const {error} = validateCourse(req.body);
    if(error){
        res.status(400).send(error)
        return;
    } 

    course.name = req.body.name;
    res.send(course);


});

app.post('/api/courses',(req,res)=>{

    const {error} = validateCourse(req.body);
    if(error){
        res.status(400).send(error)
        return;
    } 
    const course ={
        id: courses.length + 1,
        name : req.body.name
    };
    courses.push(course);
    res.send(course);
});
app.listen(port,()=>{
    console.log(" kokab your server is runing " + port)

})
// validatecourse for repetation of course
validateCourse = (course) =>{
    const schema = {
        name: Joi.string().min(3).required()
    };
    return result = Joi.validate(course, schema); 
}
// terminal error
    C:\Users\DELL\Desktop\node> node app
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::6000
    at Server.setupListenHandle [as _listen2] (net.js:1279:14)
    at listenInCluster (net.js:1327:12)
    at Server.listen (net.js:1414:7)
    at Function.listen (C:\Users\DELL\Desktop\node\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\DELL\Desktop\node\app.js:53:5)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1306:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Solution

Use Below command to kill the process

lsof -i:<port_number>

kill -9 <PID>

Leave a Reply

(*) Required, Your email will not be published