The EADDRINUSE error in Node.js occurs when you attempt to bind a server or socket to a port that is already in use by another process. This error indicates that the port is already occupied, and your application cannot start the server on that port until it is freed up.

To solve the EADDRINUSE error, you can take the following steps:

  1. Identify the Process Using the Port: Before taking any action, identify which process is currently using the port that your Node.js application is trying to bind to. To do this, you can use various tools and commands depending on your operating system:

    • On Linux/macOS: Open a terminal and use the lsof command to find the process using the port. Replace <PORT_NUMBER> with the port you are trying to use.

      bash
      lsof -i :<PORT_NUMBER>
    • On Windows: Open a command prompt with administrator privileges and use the netstat command to find the process using the port. Replace <PORT_NUMBER> with the port you are trying to use.

      bash
      netstat -ano | findstr :<PORT_NUMBER>
  2. Terminate the Conflicting Process: If you find that another process is using the port, you can choose to terminate that process or stop the application that is using the port. This will free up the port, allowing your Node.js application to bind to it.

  3. Change the Port in Your Node.js Application: If you don't want to terminate the other process or you need to use the port for a specific reason, you can change the port that your Node.js application uses. Update your code to listen on a different port that is not in use.

    For example, change the port in your Node.js application like this:

    javascript
    const http = require('http'); const PORT = 3001; // Change this to the desired port number const server = http.createServer((req, res) => { // Your server logic }); server.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
  4. Implement Error Handling: To handle the EADDRINUSE error gracefully in your Node.js application, you can use error handling to detect the error and take appropriate action, such as trying a different port or terminating the application.

    Here's an example using the server.listen method's error event:

    javascript
    const http = require('http'); const PORT = 3000; const server = http.createServer((req, res) => { // Your server logic }); server.on('error', (error) => { if (error.code === 'EADDRINUSE') { console.error(`Port ${PORT} is already in use. Trying a different port...`); // Implement logic to try a different port or handle the error } else { console.error('Server error:', error); } }); server.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

Remember that the EADDRINUSE error can occur for various reasons, such as running multiple instances of your application simultaneously or not properly closing the server before restarting it. Always ensure proper management of your server instances to avoid port conflicts.

Have questions or queries?
Get in Touch