What the EADDRINUSE Error Means
The Error: Address already in use message (or EADDRINUSE, listen EADDRINUSE: address already in use) occurs when an application attempts to bind to a network port that is already being listened on by another program. In the TCP/IP stack, a single IP address and port combination can only be assigned to one process at a time. If the socket is already occupied, the operating system rejects the startup request to prevent routing conflicts for incoming traffic. You will most commonly encounter this issue when running local development servers, databases (MySQL, PostgreSQL), proxy servers, or Docker containers.
Common Causes
- Conflict with a system service. Standard ports (80, 443, 3306, 5432) are often occupied by web servers, databases, or utilities like IIS and Apache.
- Stuck background process. The application may have crashed, but its process remains in memory and hasn't released the network socket.
TIME_WAITstate. After a connection closes, the OS temporarily reserves the port (usually for 1–4 minutes) to process any remaining buffered packets.- Concurrent execution. You accidentally launched a second instance of the same application, and both are trying to bind to the same port.
How to Fix It
Method 1: Diagnose via Terminal
First, you need to identify exactly which process is blocking the port. Open your command prompt or terminal and run the command appropriate for your OS. Replace 8080 with your actual port number.
Windows (PowerShell / CMD run as Administrator):
netstat -ano | findstr :8080
The output will show the LISTENING status and the PID (in the last column). Note this number.
Linux / macOS:
sudo lsof -i :8080
# or
sudo ss -tulpn | grep :8080
The command will display the process name and its PID under the PID/Program name column.
Method 2: Safely Terminate the Blocking Process
Once you have the process identifier, you can forcefully stop it.
⚠️ Important: Ensure the process you are terminating is not a critical system component (e.g.,
svchost.exeorsystemd-resolved). Stopping these services can disrupt network connectivity.
Windows:
taskkill /PID 1234 /F
The /F flag ensures forceful termination even if the process is unresponsive.
Linux / macOS:
kill -9 1234
If the service is managed by a process manager, it's better to use the standard commands: sudo systemctl stop <service_name>.
Method 3: Reassign the Port in Configuration
If freeing the port isn't possible or it's required by another critical application, change the listening port in your application's configuration.
- Open the configuration file (e.g.,
docker-compose.yml,.env,config.json, orserver.js). - Locate the
port,PORT, orLISTEN_PORTparameter. - Change the value to an available port (e.g.,
8081or3001). - Restart the application.
For most Node.js/Python projects, it's enough to set an environment variable before running:
export PORT=3001 && node server.js # Linux/macOS
set PORT=3001 && node server.js # Windows CMD
$env:PORT="3001"; node server.js # Windows PowerShell
Prevention
To avoid encountering this error again, follow these best practices:
- Use dynamic port allocation. For local development, configure your applications to automatically select an available port (using flags like
--port 0or settingport: "auto"). - Handle shutdown gracefully. In your server code, always attach a
SIGINT/SIGTERMsignal handler that properly closes connections (server.close()) instead of abruptly killing the process. - Check ports before deployment. Add a preliminary port availability check to your startup scripts using
nc -z localhost <port>or the PowerShell equivalentTest-NetConnection. - Isolate environments. Run your projects in Docker containers with port mapping (
-p 8080:3000) to avoid conflicts on the host machine.