Error: listen EADDRINUSE: address already in use :::5000 in Node

Dung Do Tien Jul 22 2022 27

Hi you guys. I am a beginner in Nodejs and today I tried to create a small app, I want to call an API and display all data as a table in the browser.

My server.js file

 import fetch from "node-fetch";
import express from "express";
var app = express();

app.get('/', async (req, res) =>  {
        const result = await getUser();
        console.log(result);
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
});


async function getUser() {
   try {
      const response = await fetch('https://randomuser.me/api/');
  
      if (!response.ok) {
        throw new Error(`Error! status: ${response.status}`);
      }
  
      const result = await response.json();
      return result;
   } catch (err) {
     console.log(err);
   }
}

I run the app by using the command node server.js but I got an exception throw Error: listen EADDRINUSE: address already in use :::5000. You can see detail here:

 C:\Users\conta\source\repos\NodeJs\Demo1>node server.js
node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::5000
    at Server.setupListenHandle [as _listen2] (node:net:1372:16)
    at listenInCluster (node:net:1420:12)
    at Server.listen (node:net:1508:7)
    at Function.listen (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\application.js:635:24)
    at file:///C:/Users/conta/source/repos/NodeJs/Demo1/server.js:10:18
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1399:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 5000
}

I am using Node version 16.15.1

Thank you for any suggestions.

Have 2 answer(s) found.
  • E

    Elsen Askerov Jul 22 2022

    The error: listen EADDRINUSE: address already in use :::5000 occurs because port 5000 is being used by another app. 

    Solution: You can change port 5000 to another port (ex: 5001).

    Change

     var server = app.listen(5000, function () {
        console.log('Server is running..');
    });

    To

     var server = app.listen(5001, function () {
        console.log('Server is running..');
    });

    This is a fast solution to solve it. 

  • K

    Kase Jul 22 2022

    You can kill port 5000 before using it. To kill port 5000 you can follow this:

    First, you would want to know which process is using port 5000

     sudo lsof -i :5000

    This will list all PID listening on this port, once you have the PID you can terminate it with the following:

     kill -9 {PID}

    And then you can rerun your app.

    I hope this work for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close