Error: listen EADDRINUSE

Loading

Error: listen EADDRINUSE – Address Already in Use

The listen EADDRINUSE error in Node.js occurs when a process attempts to bind to a port that is already in use. This commonly happens when running a server application that fails to shut down properly or if another application is already using the same port.


Understanding the Error

  • EADDRINUSE stands for “Error: Address in Use”.
  • It occurs when a server tries to bind to a port that is already occupied by another process.
  • This error is commonly seen in Node.js applications when using http.createServer(), express(), or net.createServer().

Step-by-Step Debugging and Solutions

1. Identify the Process Using the Port

Before fixing the issue, determine which process is using the port.

On Linux/macOS

Run the following command in the terminal:

lsof -i :PORT

Example: If the server is trying to use port 3000, run:

lsof -i :3000

You will see output similar to:

COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node     1234   user   22u  IPv6  65432      0t0  TCP *:3000 (LISTEN)

The PID (Process ID) is 1234.

To terminate the process, use:

kill -9 1234

On Windows

Run:

netstat -ano | findstr :PORT

Example:

netstat -ano | findstr :3000

It will output something like:

TCP    0.0.0.0:3000    0.0.0.0:0    LISTENING    1234

To stop the process:

taskkill /PID 1234 /F

2. Restart the Application

After terminating the process, restart your Node.js application:

node server.js

or

npm start

3. Use a Different Port

If you don’t want to terminate the running process, change the port in your application.

Example (Express.js)

Modify your server.js:

const PORT = process.env.PORT || 3001; // Use a different port if 3000 is in use
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

Run:

PORT=3001 node server.js

4. Automatically Kill Existing Process Before Restarting

Modify your start script in package.json:

"scripts": {
  "start": "kill -9 $(lsof -t -i:3000) && node server.js"
}

Now, running npm start will kill the previous process before starting a new one.


5. Handle Errors in Your Code

Modify your server setup to handle port errors gracefully:

const app = require("express")();
const PORT = process.env.PORT || 3000;

const server = app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

server.on("error", (err) => {
  if (err.code === "EADDRINUSE") {
    console.error(`Port ${PORT} is already in use!`);
    process.exit(1);
  }
});

6. Check for Orphaned Processes After Restart

If you’re using nodemon, it might leave orphaned processes after multiple restarts. To prevent this:

pkill -f node

This will kill all running Node.js processes.


7. Use a Process Manager (PM2)

PM2 helps manage your application and avoids orphaned processes:

npm install -g pm2
pm2 start server.js
pm2 list  # Check running processes
pm2 stop server  # Stop the process
pm2 delete server  # Remove it from PM2

8. Restart the Machine

If none of the above works, try restarting your computer to free up system resources.


Leave a Reply

Your email address will not be published. Required fields are marked *