How to Fix "Port Already in Use" Errors (macOS, Linux, Windows)

❌ The Error You're Seeing

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1345:16)
    ...
    
Or:
Port 3000 is already in use.
Or:
bind: address already in use

This happens when another process is already using the port you're trying to use. Here's how to find and kill that process.

Quick Fix (TL;DR)

macOS/Linux
Windows

Quick Command

# Find and kill process using port 3000
lsof -ti:3000 | xargs kill -9

Replace 3000 with your port number. That's it!

Quick Command (PowerShell)

# Find and kill process using port 3000
Get-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess | Stop-Process

Replace 3000 with your port number.

Understanding the Problem

When you start a server (Node.js, Python, Ruby, etc.), it binds to a specific port. If another process is already using that port, your server can't start.

Common causes:

Solution 1: Find and Kill the Process (Recommended)

macOS/Linux
Windows

Step 1: Find the Process ID (PID)

# Find what's using port 3000
lsof -i :3000

# Output example:
# COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
# node    12345  user   20u  IPv6 123456      0t0  TCP *:3000 (LISTEN)

The PID is 12345 in this example.

Step 2: Kill the Process

# Kill the process (replace 12345 with your PID)
kill -9 12345

# Or kill all processes on port 3000
lsof -ti:3000 | xargs kill -9

💡 Pro Tip

Add this to your ~/.zshrc or ~/.bashrc for easy access:

# Kill process on port
killport() {
  lsof -ti:$1 | xargs kill -9
  echo "Killed process on port $1"
}

# Usage: killport 3000

Step 1: Find the Process ID (PID)

# In Command Prompt or PowerShell
netstat -ano | findstr :3000

# Output example:
# TCP    0.0.0.0:3000    0.0.0.0:0    LISTENING    12345

The PID is 12345 (last column).

Step 2: Kill the Process

# Kill the process (replace 12345 with your PID)
taskkill /PID 12345 /F

# Or in PowerShell (one-liner)
Get-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess | Stop-Process

💡 Pro Tip

Create a PowerShell function in your profile:

# Add to $PROFILE
function Kill-Port($port) {
    Get-Process -Id (Get-NetTCPConnection -LocalPort $port).OwningProcess | Stop-Process
    Write-Host "Killed process on port $port"
}

# Usage: Kill-Port 3000

Solution 2: Use a Different Port

If you can't kill the process (or don't want to), just use a different port:

Node.js / Express

// Change from:
const PORT = 3000;

// To:
const PORT = process.env.PORT || 3001;  // Try 3001 instead

React (Create React App)

# In package.json, change the start script:
"scripts": {
  "start": "PORT=3001 react-scripts start"
}

# Or create a .env file:
PORT=3001

Next.js

# In package.json:
"scripts": {
  "dev": "next dev -p 3001"
}

# Or use environment variable:
PORT=3001 npm run dev

Python (Flask)

# Change from:
app.run(port=3000)

# To:
app.run(port=3001)

Solution 3: Use fkill (Cross-Platform Tool)

fkill-cli is a cross-platform tool that makes killing processes easy:

# Install globally
npm install -g fkill-cli

# Kill process on port 3000
fkill :3000

# Interactive mode (shows all processes)
fkill

💡 Why fkill is Great

  • Works on macOS, Linux, and Windows
  • Can kill by port, name, or PID
  • Interactive mode for browsing processes
  • Force kill option for stubborn processes

Solution 4: Restart Your Computer

If nothing else works, restart your computer. This will kill all processes and free up all ports.

⚠️ Last Resort

Only do this if the other solutions don't work. It's overkill for most situations.

Preventing Port Conflicts

1. Always Stop Servers Properly

Use Ctrl+C in the terminal where the server is running. Don't just close the terminal window.

2. Use Process Managers

Tools like pm2 (Node.js) or supervisor manage processes for you:

# Install pm2
npm install -g pm2

# Start your app
pm2 start app.js

# Stop your app
pm2 stop app

# List all processes
pm2 list

3. Use Environment Variables for Ports

# In your app:
const PORT = process.env.PORT || 3000;

# Then run with:
PORT=3001 node app.js

4. Check Before Starting

Add a check to your startup script:

#!/bin/bash
# start.sh

PORT=3000

if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null ; then
    echo "Port $PORT is already in use. Killing process..."
    lsof -ti:$PORT | xargs kill -9
fi

npm start

Common Ports and Their Default Uses

Port Common Use Alternative
3000 React, Node.js dev 3001, 3002
8080 HTTP alt, Tomcat 8081, 8082
5000 Flask, Docker 5001, 5002
8000 Django, FastAPI 8001, 8002
4200 Angular 4201, 4202

Troubleshooting

"lsof: command not found" (Linux)

# Install lsof
sudo apt-get install lsof    # Ubuntu/Debian
sudo yum install lsof        # CentOS/RHEL
sudo dnf install lsof        # Fedora

"Permission denied" when killing process

# Use sudo (macOS/Linux)
sudo kill -9 12345

# Or run as Administrator (Windows)

Process keeps coming back

Some processes are managed by system services. Check:

# macOS
launchctl list | grep your-app

# Linux
systemctl status your-app

# Stop the service instead
sudo systemctl stop your-app

Summary

Port conflicts are annoying but easy to fix:

  1. Quick fix: lsof -ti:3000 | xargs kill -9 (macOS/Linux)
  2. Alternative: Use a different port (3001, 3002, etc.)
  3. Cross-platform: Use fkill-cli
  4. Prevention: Always stop servers properly with Ctrl+C

Bookmark this guide for next time you see "Port already in use"!

W

Wivrix Team

We write practical guides to help developers solve real problems. No fluff, just solutions that work.