🔧 Error Fixes
· 2 min read
Last updated on

Node.js: EADDRINUSE — Port Already in Use — How to Fix It


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

What causes this

Another process is already listening on the port your app is trying to use. Only one process can bind to a specific port at a time. This happens when:

  • A previous instance of your app is still running (you forgot to stop it, or it crashed without releasing the port)
  • Another app is using the same port (e.g., another Node server, a Docker container, or a system service)
  • You restarted your app too quickly and the OS hasn’t released the port yet

Fix 1: Find and kill the process using the port

# macOS / Linux — find what's on port 3000
lsof -i :3000

# You'll see output like:
# COMMAND  PID  USER  FD  TYPE  DEVICE  SIZE/OFF  NODE  NAME
# node     1234 user  22u IPv6  ...     0t0       TCP   *:3000

# Kill it
kill 1234
# Or force kill if it won't stop
kill -9 1234

On Windows:

netstat -ano | findstr :3000
taskkill /PID 1234 /F

Fix 2: Use a different port

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

Or set it via environment variable:

PORT=3001 node app.js

Fix 3: Add graceful shutdown

Prevent the issue from happening again by properly closing the server when the process exits:

const server = app.listen(3000);

// Handle Ctrl+C
process.on('SIGINT', () => {
  server.close(() => {
    console.log('Server shut down gracefully');
    process.exit(0);
  });
});

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

Fix 4: Kill all Node processes (nuclear option)

If you can’t find the specific process:

# Kill all Node.js processes
killall node

# Or on macOS/Linux
pkill -f node

⚠️ This kills ALL Node processes, including other apps. Use as a last resort.

Fix 5: Use a port-finding utility

Automatically find an available port:

import { createServer } from 'net';

function getAvailablePort(preferred) {
  return new Promise((resolve) => {
    const server = createServer();
    server.listen(preferred, () => {
      const { port } = server.address();
      server.close(() => resolve(port));
    });
    server.on('error', () => resolve(getAvailablePort(preferred + 1)));
  });
}

const port = await getAvailablePort(3000);
app.listen(port);

How to prevent it

  • Always implement graceful shutdown handlers (SIGINT, SIGTERM) in your Node apps
  • Use a process manager like PM2 that handles restarts and port management
  • Use nodemon for development — it properly kills the old process before restarting
  • Set the port via environment variable so you can easily change it without modifying code