More Node.js answers

by Danny
Published 3 min read

Node.js throws EADDRINUSE when another process already holds the port your server wants. Here is how to find that process, free the port, and stop it happening again.

The short answer

#

Something else is already listening on the port your server is trying to use. Usually it is an older copy of your own app that never shut down. Find the process holding the port, stop it, then start your server again.


_10
# macOS and Linux: find what is using port 3000
_10
lsof -i :3000
_10
_10
# stop it using the PID from the output
_10
kill 12345

Why it happens

#

Only one process can listen on a given address and port at a time. When you call server.listen(3000) (or a framework does it for you), Node.js asks the operating system for that port. If another process already has it, the operating system refuses and Node.js reports the failure as an error with the code EADDRINUSE:


_10
Error: listen EADDRINUSE: address already in use :::3000

The :::3000 part means the IPv6 "any address" on port 3000. The most common causes are:

  • A previous run of your server is still alive. This happens when a terminal tab was closed without stopping the process, when a watcher such as node --watch or nodemon restarted before the old process exited, or when a debugger kept the process paused.
  • Two of your own services are configured with the same port, for example an API and a front end dev server both defaulting to 3000.
  • A completely different program owns the port. On macOS, the AirPlay Receiver uses port 5000 and 7000, which catches out a lot of people who pick 5000 for a local API.
  • Your code calls listen() twice, for example once in the app module and again in a test setup file.

How to fix it

#

Find and stop the process holding the port

On macOS and Linux, lsof lists the process and its PID:


_10
lsof -i :3000
_10
# COMMAND PID USER FD TYPE ... NAME
_10
# node 12345 you 23u IPv6 ... *:3000 (LISTEN)
_10
_10
kill 12345

If the process ignores a normal kill, check that it is really the one you expect before forcing it with kill -9 12345.

On Windows, use netstat to find the PID and taskkill to stop it:


_10
netstat -ano | findstr :3000
_10
taskkill /PID 12345 /F

Read the command name in the output before you stop anything. If it is not a process you started, pick a different port for your app instead.

Use a different port

Make the port configurable so you can move out of the way without editing code:


_10
const port = Number(process.env.PORT) || 3000;
_10
_10
server.listen(port, () => {
_10
console.log(`Listening on http://localhost:${port}`);
_10
});


_10
PORT=3001 node server.js

Most frameworks and dev servers read PORT too, so this is usually the quickest way to run two projects side by side.

Handle the error in your own code

By default the error is thrown as an unhandled error event and the process exits with a long stack trace. Listen for it to give a clearer message:


_13
import http from 'node:http';
_13
_13
const server = http.createServer(app);
_13
_13
server.on('error', (err) => {
_13
if (err.code === 'EADDRINUSE') {
_13
console.error(`Port ${port} is already in use. Stop the other process or set PORT.`);
_13
process.exit(1);
_13
}
_13
throw err;
_13
});
_13
_13
server.listen(port);

Let the operating system choose a port

For tests, pass 0 as the port. The operating system picks a free one and you read it back afterwards, so parallel test runs never collide:


_10
server.listen(0, () => {
_10
const { port } = server.address();
_10
console.log(`Test server on port ${port}`);
_10
});

Shut down cleanly

If old copies of your server keep hanging around, close the server when the process is asked to stop. This lets watchers restart without fighting over the port:


_10
function shutdown() {
_10
server.close(() => process.exit(0));
_10
}
_10
_10
process.on('SIGINT', shutdown);
_10
process.on('SIGTERM', shutdown);

server.close() stops accepting new connections and waits for open ones to finish. If you have long-lived keep-alive connections, server.closeAllConnections() ends them so the close does not hang.

Common pitfalls

#
  • Stopping the wrong process. Always check the command name and PID before running kill or taskkill, especially with -9 or /F.
  • Closing the terminal window and assuming the server stopped. Some terminals and IDEs leave child processes running in the background.
  • Picking port 5000 or 7000 on macOS while AirPlay Receiver is on. Either choose another port or turn the receiver off in System Settings.
  • Calling listen() inside the module that tests import. Export the app or server and call listen() only in the entry file.
  • Running in Docker and getting the error on the host. The clash can be on the published host port in your docker run -p or Compose file, not inside the container.
#

Was this resource helpful?