Port Already in Use
The application cannot start because the required port is already occupied by another process.
Explanation
Port Already in Use occurs when an application tries to bind to a TCP/UDP port that is already in use by another process. The error code EADDRINUSE (Address Already In Use) indicates the port is occupied. Common causes include a previous instance of the same application not properly shut down, another application using the same port, zombie processes holding the port, or starting multiple instances of a server without configuring port reuse. This is common in development when restarting applications.
Common Causes
- Previous instance not properly shut down
- Another application using same port
- Zombie processes holding the port
- Multiple server instances started
- Docker container still running
Solution
Find the process using the port: lsof -i :3000 (Linux/Mac) or netstat -ano | findstr :3000 (Windows). Kill the process: kill -9 PID or taskkill /PID PID /F (Windows). Configure your application to use a different port. Use SO_REUSEADDR socket option in your application. For Node.js, use process.on('SIGTERM') to properly close the server. For Docker, stop the container using the port: docker stop container_name. Use pm2 for Node.js process management which handles cleanup. Check for orphaned processes after crashes.
Code Example
# Find what's using port 3000
lsof -i :3000
# COMMAND PID USER FD TYPE DEVICE NODE NAME
# node 1234 user 23u IPv4 ... TCP *:3000 (LISTEN)
# Kill the process
kill -9 1234
# Or on Windows
netstat -ano | findstr :3000
taskkill /PID 1234 /F
# Use a different port
PORT=3001 npm start
# Docker: stop container using port
docker stop $(docker ps -q --filter publish=3000)
# Node.js: handle graceful shutdown
const server = app.listen(3000);
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
});
# Use pm2 for process management
pm2 start app.js --name myapp
pm2 restart myapp
# Check for zombie processes
ps aux | grep zombie
ps -ef | grep defunct
Error Information
Language
javascript
Framework
nodejs
Difficulty
Beginner
Views
13
Related Errors
JavaScript
SyntaxError: Unexpected Token
JavaScript parser encountered a token it didn't expect at that position.
Frontend
Invalid DOM Property
The code uses an incorrect HTML attribute or React-specific prop on a DOM element.
JavaScript
RangeError: Maximum Call Stack Size Exceeded
The function calls itself recursively too many times, exceeding the stack size limit.
Frontend
Content Security Policy Violation
The browser blocked a resource because it violated the Content Security Policy.