The error by language
Every language throws this error differently, but the cause is always the same: another process is bound to the port.
| Language | Error message |
|---|---|
| Node.js | Error: listen EADDRINUSE: address already in use :::3000 |
| Python | OSError: [Errno 98] Address already in use |
| Go | listen tcp :8080: bind: address already in use |
| Java | java.net.BindException: Address already in use |
| Ruby | Errno::EADDRINUSE: Address already in use - bind(2) |
| .NET | SocketException: Address already in use |
| Rust | AddrInUse: Address already in use |
Universal fix: portrm
Regardless of which language threw the error, the fix is the same:
$ ptrm fix 3000
Port 3000 in use
node (PID 48291) - Next.js dev server
SAFE - development server
Killed PID 48291. Port 3000 is free.
Fix by language
Node.js (EADDRINUSE)
// Handle gracefully
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error(`Port ${port} in use. Run: ptrm fix ${port}`);
process.exit(1);
}
});
Python (OSError)
# Enable SO_REUSEADDR
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', 8000))
# Flask: use a different port
app.run(port=8001)
# Django: specify port
$ python manage.py runserver 8001
Go
// Check error type
ln, err := net.Listen("tcp", ":8080")
if err != nil {
if opErr, ok := err.(*net.OpError); ok {
if sysErr, ok := opErr.Err.(*os.SyscallError); ok {
if sysErr.Err == syscall.EADDRINUSE {
log.Fatal("Port 8080 in use. Run: ptrm fix 8080")
}
}
}
log.Fatal(err)
}
Java (BindException)
// Spring Boot: change port
// application.properties
server.port=9090
// Or catch the exception
try {
ServerSocket ss = new ServerSocket(8080);
} catch (BindException e) {
System.err.println("Port 8080 in use. Run: ptrm fix 8080");
}
Ruby
# Rails: use a different port
$ rails server -p 3001
# Sinatra
set :port, 3001
Manual fix (all OS)
# macOS / Linux
$ lsof -i :3000
$ kill -9 <PID>
# Windows
> netstat -ano | findstr :3000
> taskkill /PID <PID> /F
Prevention
Use ptrm preflight in your start scripts to check port availability before launching:
# package.json
{
"scripts": {
"dev": "ptrm preflight 3000 && next dev",
"start:api": "ptrm preflight 8080 && node server.js"
}
}
Same fix, every language
portrm works regardless of which language or framework threw the error. Find and free any port in one command.