Node.js4 min read

Error: listen EADDRINUSE - Port Already in Use in Node.js

Getting EADDRINUSE error? Your port is taken. Here's how to find what's using it and fix it properly.

Mike Rodriguez
December 18, 2025
15.6k478

When you start your Node.js server and see this error, another process is already using that port.

Error Message:

Error: listen EADDRINUSE: address already in use :::3000

Quick Solution

Mac/Linux

Find the process:

lsof -i :3000

Output shows:

node    12345 user   23u  IPv6 ... TCP *:3000 (LISTEN)

Kill it:

kill -9 12345

One-liner:

kill -9 $(lsof -t -i:3000)

Windows

Find the process:

netstat -ano | findstr :3000

Kill it:

taskkill /PID 12345 /F

Better Solution: Auto Port Selection

Instead of hardcoding port 3000, try the next available port:

const http = require('http');

function startServer(port) {
  const server = http.createServer(app);
  
  server.listen(port)
    .on('listening', () => {
      console.log(`Server on port ${port}`);
    })
    .on('error', (err) => {
      if (err.code === 'EADDRINUSE') {
        console.log(`Port ${port} busy, trying ${port + 1}`);
        startServer(port + 1);
      }
    });
}

startServer(3000);

Environment Variables

Use flexible port configuration:

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

Run with custom port:

PORT=3001 node server.js

Common Causes

Cause Solution
Previous server still running Kill the process
Multiple server instances Check running processes
Docker container active Stop container
nodemon didn't clean up Restart nodemon

Proper Server Cleanup

Add signal handlers to close server properly:

const server = app.listen(3000);

process.on('SIGINT', () => {
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});

process.on('SIGTERM', () => {
  server.close(() => {
    process.exit(0);
  });
});

Now Ctrl+C properly shuts down the server.

NPM Script Helper

Add to package.json:

{
  "scripts": {
    "kill-port": "kill -9 $(lsof -t -i:3000) 2>/dev/null || true",
    "start": "npm run kill-port && node server.js"
  }
}

Docker Issues

If using Docker and port is still busy:

docker ps                    # List containers
docker stop <container-id>   # Stop specific container
docker rm <container-id>     # Remove container

Prevention Tips

  1. Always handle SIGINT and SIGTERM signals
  2. Use environment variables for ports
  3. Implement auto port selection for development
  4. Clean up Docker containers regularly
  5. Use process managers like PM2 in production

This error usually means a previous instance didn't shut down cleanly. The solutions above handle both the immediate fix and prevention.

#Node.js#Error#Port#EADDRINUSE