Fix "Error: listen EADDRINUSE: address already in use" in Node.js
Error: listen EADDRINUSE: address already in use :::3000
- Error: listen EADDRINUSE: address already in use 127.0.0.1:3000
- Error: listen EADDRINUSE: address already in use 0.0.0.0:8080
- Error: listen EADDRINUSE :::3000
- Error: Port 5173 is already in use
- code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '::', port: 3000
The port your Node.js server wants to listen on (here 3000) is already taken, most often by an old instance of your own server that is still running. Find the process using it (lsof -i :3000 on macOS/Linux, netstat -ano | findstr :3000 on Windows), stop it, or start the app on another port.
What this error means
When you call server.listen(3000) or app.listen(3000), Node.js asks the operating system to reserve that TCP port. A port can only be listened on by one process at a time on the same address. If it is already taken, the system returns the EADDRINUSE code and Node.js throws the error, then exits if it is not handled. :::3000 means "every IPv6 and IPv4 address, port 3000". The errno number depends on the OS (-98 on Linux, -48 on macOS, -4091 on Windows), but the cause is always the same: another process holds the port.
Common causes
- Old instance still running: server started in another terminal, orphaned
nodemonprocess, or terminal closed without stopping the server. - Process suspended with Ctrl+Z: on macOS and Linux, Ctrl+Z pauses the process without exiting, and it keeps the port.
- Another application on the same port: another project, a Docker container publishing that port, or a system service (on macOS, AirPlay Receiver uses ports 5000 and 7000).
listencalled twice in the code: for example in bothapp.jsandserver.js, or in every test file that imports the app.
Step-by-step fix
-
Find the process holding the port
Replace 3000 with your port. The command gives the PID (process id) and its name.
# macOS / Linux lsof -i :3000 # Linux (alternative) sudo ss -ltnp 'sport = :3000' # Windows (cmd or PowerShell) netstat -ano | findstr :3000 tasklist /FI "PID eq 12345" -
Stop the process
If it is an old instance of your server, stop it with its PID. Try a normal stop first, and only force it (
-9,/F) if it does not respond. Check the process name first: do not kill a system service or someone else’s application.# macOS / Linux kill 12345 kill -9 12345 # Windows (cmd) taskkill /PID 12345 /F # Windows (PowerShell), by port Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000 -State Listen).OwningProcess -
Resume a suspended process (macOS / Linux)
If you pressed Ctrl+Z, the server is paused in the same terminal. List the jobs, bring it back to the foreground, then quit it with Ctrl+C.
jobs fg %1 -
Make the port configurable
Read the port from an environment variable so you can change it without touching the code.
const port = Number(process.env.PORT) || 3000; app.listen(port, () => console.log(`Listening on http://localhost:${port}`)); // macOS / Linux: PORT=3001 npm start // Windows (PowerShell): $env:PORT=3001; npm start -
Call
listenonly onceExport the app without starting the server, and call
listenin a single entry file. Tests (for example withsupertest) then importappwithout opening a port.// app.js import express from 'express'; export const app = express(); // server.js import { app } from './app.js'; app.listen(process.env.PORT || 3000); -
Handle the error cleanly
Listen to the server’s
errorevent to print a clear message instead of a stack trace.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; });
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Always stop your server with Ctrl+C rather than Ctrl+Z or by closing the terminal window.
- Use the
PORTvariable and a.envfile per project so two local projects do not target the same port. - Handle
SIGINTandSIGTERMto close the server (server.close()) cleanly, especially withnodemonand in Docker.
Frequently asked questions
Why is port 5000 taken on my Mac?
Since macOS Monterey, AirPlay Receiver listens on ports 5000 and 7000. Change your app’s port, or turn off "AirPlay Receiver" in System Settings > General > AirDrop & Handoff.
lsof returns nothing but the port is still taken, what now?
The process may belong to another user or to root: run sudo lsof -i :3000 again. Also check docker ps, since a container that publishes this port holds it on your machine.
How do I free a port in one command on macOS or Linux?
kill $(lsof -ti :3000) stops every process using port 3000. Check what they are first with lsof -i :3000.
Useful tools for this error
More JavaScript & Node.js errors
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "Error: Cannot find module" in Node.js
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "SyntaxError: Cannot use import statement outside a module"