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
200
Related Errors
JavaScript
TypeError: X is not a function
The code tries to call a value as a function, but it's not a function.
Database
TypeORM/SQLAlchemy EntityNotFoundError
The ORM cannot find the requested entity or model in the database.
General
Circular Dependency Detected
Module A depends on B which depends on A, creating an unresolvable import cycle.
ERR_REQUIRE_ESM
Node.js
ERR_REQUIRE_ESM
Node.js cannot require() an ES module; use import() instead.