debugjavascriptMajor
EADDRINUSE: port already in use
Viewed 0 times
EADDRINUSEport in usekill processlsofaddress in use
nodejs
Error Messages
Problem
Starting a server fails with Error: listen EADDRINUSE: address already in use :::3000. Another process is using the port, or a previous instance didn't shut down cleanly.
Solution
Find and kill the process using the port:
# Find what's using the port
lsof -i :3000 # macOS/Linux
netstat -ano | findstr :3000 # Windows
# Kill it
kill -9 $(lsof -t -i :3000) # macOS/Linux
# In Node.js, handle graceful shutdown
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
});
# Use a different port if needed
const port = process.env.PORT || 3001;
# Or set SO_REUSEADDR
const server = app.listen(port);
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error(
server.listen(port + 1);
}
});
# Find what's using the port
lsof -i :3000 # macOS/Linux
netstat -ano | findstr :3000 # Windows
# Kill it
kill -9 $(lsof -t -i :3000) # macOS/Linux
# In Node.js, handle graceful shutdown
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
});
# Use a different port if needed
const port = process.env.PORT || 3001;
# Or set SO_REUSEADDR
const server = app.listen(port);
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error(
Port ${port} in use, trying ${port + 1});server.listen(port + 1);
}
});
Why
Only one process can bind to a TCP port at a time. If a previous server crashed without closing the socket, the OS may keep the port in TIME_WAIT state for up to 60 seconds.
Gotchas
- On macOS, AirPlay uses port 5000 by default
- Docker containers bind to host ports — check docker ps
- After a crash, the port may be in TIME_WAIT for 60 seconds
- nodemon/ts-node-dev handle restarts but can leave zombie processes
Code Snippets
Find and kill process on port
# Find process on port 3000
lsof -i :3000
# Kill it
kill -9 $(lsof -t -i :3000)Context
When starting a Node.js server and the port is already occupied
Revisions (0)
No revisions yet.