# Error: listen EADDRINUSE: address already in use :::3000 (Node.js)

Thrown as an unhandled 'error' event the moment server.listen() runs, so the app exits during startup. The ':::3000' part is the IPv6 wildcard address, which on most systems also covers IPv4.

```
node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1908:16)
    at listenInCluster (node:net:1965:12)
    at Server.listen (node:net:2067:7)
    at file:///app/server.js:14:8 {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
```

## Quick fix

Find and stop whatever already holds port 3000, then start the server again. On macOS or Linux, lsof prints the PID of the listener; kill it, or pick a different PORT. If an orphaned dev server keeps coming back, fix the restart flow that leaves the old process alive.

1. Run lsof -nP -iTCP:3000 -sTCP:LISTEN (macOS/Linux) or netstat -ano | findstr :3000 (Windows).
2. Check what that PID is with ps -p <PID> -o command.
3. Stop it with kill <PID>; use kill -9 only if it ignores SIGTERM.
4. Read the port from process.env.PORT so switching needs no code change.
5. Handle SIGTERM and SIGINT by calling server.close() so restarts release the port.

```bash
# Who is listening on 3000?
lsof -nP -iTCP:3000 -sTCP:LISTEN

# Ask it to exit (replace 12345 with the PID from above)
kill 12345

# Linux alternative: kill whatever owns the port
fuser -k 3000/tcp
```

## Why it happens

### An old copy of the server is still running

A closed terminal tab, a crashed editor or a detached watcher (nodemon, tsx watch) can leave the previous server alive in the background. That process still owns the listening socket, so the new one cannot bind to the same port.

### A child process survived its parent

Tools that spawn a worker to serve requests (next start, cluster workers, some dev servers) can leave the child running when only the parent gets killed. Process managers need tree-kill behavior, otherwise the orphaned child keeps the port.

### Another program already uses that port

A Docker container published with -p 3000:3000, a second project, or a system service is listening there. On recent macOS versions, AirPlay Receiver occupies ports 5000 and 7000, which trips up Flask-style defaults and some Node setups.

### listen() called twice in the same process

The app module calls app.listen() at import time and the entry file or test suite calls it again. Hot-reload setups that re-run the module without closing the previous server hit the same wall on every reload.

### Overlapping restarts in production

A deploy script starts the new process before the old one has released the port, or two pm2 or systemd units point at the same app. Without a graceful shutdown, the old process can hold on for its full kill timeout.

## Other fixes

- In tests, export the app without calling listen and use listen(0) so each test file gets a free random port.
- Under pm2, enable treekill with a kill_timeout so worker children die with the parent.
- Listen for server.on('error') and print a clear 'port in use' message instead of the raw unhandled event.
- Turn off AirPlay Receiver in macOS System Settings if it holds port 5000 or 7000.

## Same error, other wording

- `Error: listen EADDRINUSE :::3000`
- `Error: listen EADDRINUSE: address already in use 0.0.0.0:3000`
- `Error: listen EADDRINUSE: address already in use 127.0.0.1:8080`

## Sources

- [Node.js net module: server.listen()](https://nodejs.org/api/net.html)
- [Node.js errors: common system errors](https://nodejs.org/api/errors.html)

---

To see this error the next time it happens in production, with the stack trace and the steps before it: Runheed records it and serves it to your coding agent over MCP. Setup instructions an agent can follow: https://runheed.com/SETUP.txt

Human-readable version: https://runheed.com/errors/eaddrinuse
