Node.js

EADDRINUSE: address already in use, and who is holding the port

Nothing is broken in your code: another process already owns the port, and very often it is an older copy of your own app. Here is how to find it on any operating system, why it keeps happening on restart, and how a clean shutdown ends it.

You start the app and it dies before serving a single request: Error: listen EADDRINUSE: address already in use :::3000. Nothing in your code changed. Another process already owns port 3000, and surprisingly often that process is an older copy of the app you are trying to start.

What does EADDRINUSE mean?

It means server.listen() asked the operating system for a port that another socket is already listening on, and the operating system said no. The error object carries code: 'EADDRINUSE' and syscall: 'listen'. In :::3000, the :: part means "every address on this machine", which is what Node binds to when you give it a port and no host.

Because the server never starts, there is no request, no route and no stack trace into your code. The fix is never in the line the error points at. It is in finding who got there first.

How do you find the process holding the port?

Ask the operating system which process is listening. On Linux:

ss -ltnp 'sport = :3000'
# or
sudo lsof -i :3000

On macOS:

lsof -nP -iTCP:3000 -sTCP:LISTEN

On Windows, in PowerShell or Command Prompt, find the process ID and then its name:

netstat -ano | findstr :3000
tasklist /FI "PID eq 12345"

The output gives you a process ID and a command. Read the command before doing anything else. A node server.js from your own project tells a very different story from a database or another company's service.

Diagram: who usually holds the port when Node reports EADDRINUSE. An older copy of the same app that never exited. The app started twice, once by a process manager such as pm2 or systemd and once by hand. Two services left on the same default port because PORT was not set. The code calling listen() twice, for example in tests that import the server. The command to find the holder: ss -ltnp on Linux, lsof -nP -iTCP:3000 -sTCP:LISTEN on macOS, netstat -ano with findstr on Windows.
Most of the time, the process holding the port is your own app.

Who is usually holding it?

1. An older copy of the same app. The previous run never exited: a dev server left running in another terminal, a crashed watcher that left its child process behind, or a restart where the new process started before the old one let go of the port.

2. The app started twice. A process manager such as pm2 or systemd is already running it, and someone started a second copy by hand to test something. The manual one fails, which is the good outcome. The bad outcome is the reverse: the manual copy grabs the port first after a reboot, and the managed one crash-loops.

3. Two services on the same default. Many apps use process.env.PORT || 3000. Deploy two of them on one server without setting PORT and both reach for 3000.

4. The code listens twice. app.listen() in both app.js and server.js, or a test suite that imports the module that starts the server once per test file. The usual fix is to export the app without listening and start it in exactly one place.

One thing that looks similar but is fine: pm2 in cluster mode, or Node's own cluster module, runs several workers on one port by design. The primary process owns the port and shares connections with the workers, so that setup does not cause EADDRINUSE. Docker has its own version of the error on the host side, port is already allocated, which means another container or host process published the same port.

How do you stop it happening on every restart?

Make the app release the port when it is asked to stop. Process managers, Docker and systemd send SIGTERM before they resort to a hard kill. Handle it: stop accepting new connections, let the open requests finish, close your database pool, then exit.

const server = app.listen(port);

function shutdown(signal) {
  console.log(signal + ' received, closing server');
  server.close(() => {
    // close database pools and queues here
    process.exit(0);
  });
  // give up after 10 seconds instead of hanging forever
  setTimeout(() => process.exit(1), 10000).unref();
}

process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));

server.close() waits for open connections to finish, and a keep-alive connection can stay open for a while, which is why the timer above sets a limit. On Node 18.2 and later, calling server.closeIdleConnections() right after server.close() releases idle keep-alive connections straight away.

Finally, make a port clash loud. Listen for the server's error event, log a clear message with the port, and exit with a non-zero code. A process manager then reports a failed start instead of an app that is mysteriously not answering.

server.on('error', (err) => {
  if (err.code === 'EADDRINUSE') {
    console.error('Port ' + port + ' is already in use. Is another copy running?');
    process.exit(1);
  }
  throw err;
});

Setup for the Node.js connector is one command, listed on the connectors page. If the process starts fine but cannot reach a local database, the cause is different: ECONNREFUSED ::1 after a Node upgrade covers it.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required