Skip to content
the poor coder

Error: listen EADDRINUSE: address already in use

Poor Man
Published
Reading time
3 min read
On this page

Error: listen EADDRINUSE: address already in use#

If you are developing an application or running a server, you might have faced this error: "Error: listen EADDRINUSE: address already in use". This error occurs when you try to run a server on a port that's already in use by another process or server.

This error message is quite helpful since it tells you what's wrong: "address already in use". However, it doesn't tell you which process or server is using the port that you are trying to use. Therefore, it's essential to know how to troubleshoot this error.

1. Identify the process that's using the port#

The first step is to find out which process or server is using the port that you are trying to use. You can use the following command in your terminal:

Text
lsof -i :[portNumber]

Replace [portNumber] with the number of the port that you are trying to use. This command will show you the process ID (PID) of the process that's listening on that port. You can then use the following command to kill the process:

Text
kill -9 [PID]

Replace [PID] with the PID number that you got from the previous command.

2. Change the port number#

If you don't want to kill the process that's using the port, you can change the port number of your server. You can do this by modifying your code and specifying a different port number.

Text
const http = require('http');

const hostname = 'localhost';
const port = 3001; // Change this to a different port number

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

3. Wait for the port to be available#

If you don't want to kill the process and don't want to change the port number, you can wait for the port to be available. You can use the following code to check if the port is available and retry after a certain amount of time:

Text
const net = require('net');

const port = 3000;

function checkPort() {
  const server = net.createServer();
  server.once('error', (err) => {
    if (err.code === 'EADDRINUSE') {
      console.log(`Port ${port} is in use, retrying in 1 second...`);
      setTimeout(checkPort, 1000);
    }
  });
  server.once('listening', () => {
    server.close();
    console.log(`Port ${port} is available!`);
  });
  server.listen(port);
}

checkPort();

This code checks if the port is available by creating a temporary server. If the port is in use, it retries after a certain amount of time. Once the port is available, it logs a message.

Conclusion#

"Error: listen EADDRINUSE: address already in use" is a common error that occurs when you try to run a server on a port that's already in use. To troubleshoot this error, you can identify the process that's using the port, change the port number, or wait for the port to be available. By using these methods, you can resolve this error and run your server without any issues.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC