Error: listen EADDRINUSE: address already in use

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:

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:

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.

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:

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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe