Node.js EADDRINUSE — Port Already In Use
Fix the EADDRINUSE (Address already in use) error in Node.js. Learn how to find and kill processes using a port, prevent the error, and handle it gracefully in your application.
Detailed Explanation
EADDRINUSE: Address Already In Use
This error occurs when a Node.js application tries to bind to a network port that is already occupied by another process. It is one of the most common development-time errors.
Finding the Process
macOS / Linux:
# Find process using port 3000
lsof -i :3000
# Output: node 12345 user 22u IPv6 ...
# Kill the process
kill -9 12345
# One-liner: find and kill
lsof -ti :3000 | xargs kill -9
Windows:
# Find process using port 3000
netstat -ano | findstr :3000
# Kill by PID
taskkill /PID 12345 /F
Common Scenarios
- Previous instance still running — Crashed server left a zombie process
- Nodemon restart race condition — New instance starts before old one fully closes
- Docker port mapping conflict — Same host port mapped by multiple containers
- Multiple dev servers — Running two projects on the same port
Prevention Strategies
1. Graceful shutdown:
const server = app.listen(3000);
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
});
process.on('SIGINT', () => {
server.close(() => process.exit(0));
});
2. Dynamic port fallback:
const server = app.listen(0); // OS assigns random available port
console.log('Listening on port', server.address().port);
3. SO_REUSEADDR:
const server = net.createServer();
server.listen({ port: 3000, exclusive: false });
Use Case
EADDRINUSE is encountered daily by developers working on Node.js applications during local development, especially when using tools like nodemon that restart the server frequently. Understanding how to properly close servers, implement graceful shutdown, and find conflicting processes saves significant development time.