EADDRINUSE
The network port or address is already in use by another process.
Usually happens because:
- ☑ Background App Hanging
- ☑ Duplicate Dev Tool Servers
- ☑ TIME_WAIT Port Locking
🔍 Quick Checklist:
What is EADDRINUSE?
EADDRINUSE stands for 'Error: Address Already in Use'. It is thrown when Node.js attempts to bind a server or listener socket to a network port or IP address that is already active and occupied by another process on your operating system.
Common Causes
- Background App Hanging: An older instance of your server failed to close properly and is still holding the port in the background.
- Duplicate Dev Tool Servers: Another dev server (e.g. React/Vite/Docker) is already listening on the requested port.
- TIME_WAIT Port Locking: The operating system has temporarily locked the port during socket recycling (TIME_WAIT).
| Cause | Frequency |
|---|---|
| Background App Hanging | ⭐⭐⭐⭐⭐ |
| Duplicate Dev Tool Servers | ⭐⭐⭐⭐ |
| TIME_WAIT Port Locking | ⭐⭐⭐ |
Common Mistakes
- Failing to close VS Code terminals or PM2 background instances before starting node processes again.
- Binding Docker container ports to host ports that are already occupied by host database services.
How to Fix
Framework-Specific Examples
Checking for EADDRINUSE errors in Express and automatically falling back to an alternative port.
const startServer = (port) => {
const server = app.listen(port, () => {
console.log(`Server successfully started on port ${port}`);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${port} is occupied. Trying port ${port + 1}...`);
startServer(port + 1);
}
});
};Server Configuration Examples
Configuring Apache to listen on alternative ports.
Listen 8080Best Practices
- Use variable PORT bindings derived from process.env.PORT configurations.
- Check active ports using automated build check scripts.
Frequently Asked Questions (FAQ)
Q: How do I find out which process is using a port on Windows?
Open PowerShell and run `netstat -ano | findstr :3000` to find the process ID (PID), then kill it using `taskkill /PID <PID> /F`.
Q: How do I find and kill a port process on Linux/macOS?
Run `lsof -i :3000` to find the process ID (PID), then terminate it using `kill -9 <PID>`.
Q: What is the TIME_WAIT socket state?
When a server terminates, the operating system keeps the port reserved for a brief duration (typically 30-120 seconds) to ensure delayed packets are received. This can trigger temporary EADDRINUSE errors.
Q: Can I bind multiple applications to the same port?
No. Only one application process can listen for incoming connections on a specific port at any given time.