skip to Main Content

As I am trying to run the webservice shown as follows:

const port = 3000;
app.get('/hello', (req, res, next) => {
    res.send('Hello, World!');
});
app.listen(port, () => {
    console.log('listening to port', port);
}); 

For the very first time, it works fine. But since then, when I run node index.js which contains the webservice mentioned above, I receive the error posted below.

To solve this issue I ran the following command:

npx kill-port 3000

but that did not solve the problem.

Please let me know if there is another way to solve this issue.

Note: I tried to use several other ports numbers, but I receive the same error message with the new port number indicated in the error message.

Error:

node:events:489
  throw er; // Unhandled 'error' event
  ^
Error: listen EADDRINUSE: address already in use :::3000

3

Answers


  1. It seems that port 3000 is already in use by another process.

    If you use linux as OS:

    • For a list of listening ports
    ss -ntlp
    # or
    netstat -ntlp
    
    • To check specifically for port 3000
    netstat -ntlp | grep ':3000'
    

    Next, kill the process using the port (check what it is first).

    kill <PID>
    kill -9 <PID>  # if kill does not work
    

    If you’re using Windows or macOS, there’s an equivalent way to list listening ports and kill the process using the desired port.

    Windows :

    netstat -ano | findstr :3000
    taskkill /PID <PID> /F
    

    MacOS:

    lsof -ti:3000
    kill <PID>
    kill -9 <PID> # if kill does not work
    
    Login or Signup to reply.
  2. As you mentioned, the very first time is works fine & then it throws an error second time onwards.

    It has to do with the way you are stopping the application if your doing Ctrl + Z (or cmd + Z on Mac) the app will be stopped on terminal but the process won’t be killed.

    The correct way is Ctrl + C (or cmd + C on Mac), it will send SIGINT which will kill the application.

    Login or Signup to reply.
  3. Stop the process from Task Manager.

    end the task

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search