Fix "Error: listen EADDRINUSE: address already in use" in Node.js
Your Node.js app crashed on start with an error like this:
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1872:16)
code: 'EADDRINUSE',
port: 3000What it means
Another process is already listening on that port, so your app can't. :::3000 means "port 3000 on all addresses". Most of the time the other process is an earlier copy of your own app.
- A dev server is still running in another terminal tab, or in the background after the terminal was closed.
- A watcher such as nodemon restarted the app before the old one finished exiting.
- Two projects are configured to use the same port.
- Another program uses that port, for example AirPlay Receiver on port 5000 on a Mac.
Fix 1: free the port
Find the process on the port and stop it. If it is an old copy of your app, that is all you need.
Get-NetTCPConnection -LocalPort 3000 -State Listen |
ForEach-Object { Stop-Process -Id $_.OwningProcess }kill $(lsof -t -iTCP:3000 -sTCP:LISTEN)These stop whatever is on the port without asking. Detailed, step-by-step versions: Windows, macOS, Linux.
Fix 2: use another port
If the other program should keep running, start yours on a different port. Most frameworks read the PORT environment variable:
PORT=3001 npm start$env:PORT=3001; npm startSome tools use a flag instead: next dev -p 3001, vite --port 5174. See port already in use for React, Next.js and Vite.
Fix 3: a clear message instead of a stack trace
Handle the error in your server so the next person sees what to do:
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT);
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Stop the other process or set PORT.`);
process.exit(1);
}
throw err;
});Stop it coming back
- Stop dev servers with Ctrl+C rather than closing the terminal window, which can leave them running.
- If it happens after every file save, the watcher is starting a new copy before the old one exits. Make sure the app closes its server on SIGTERM, or update nodemon.
- portfind shows which project each port belongs to, and warns when the same port keeps coming back after you kill it.