---
title: "Error: listen EADDRINUSE: address already in use"
description: "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."
url: https://www.danny.engineering/answer/nodejs/error-listen-eaddrinuse-address-already-in-use
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Node.js"]
---

# Error: listen EADDRINUSE: address already in use

## 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.


```bash
# macOS and Linux: find what is using port 3000
lsof -i :3000

# stop it using the PID from the output
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`:


```bash
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:


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

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:


```bash
netstat -ano | findstr :3000
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:


```javascript
const port = Number(process.env.PORT) || 3000;

server.listen(port, () => {
  console.log(`Listening on http://localhost:${port}`);
});
```


```bash
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:


```javascript
import http from 'node:http';

const server = http.createServer(app);

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;
});

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:


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


### 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:


```javascript
function shutdown() {
  server.close(() => process.exit(0));
}

process.on('SIGINT', shutdown);
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.

## Related reading

- [Node.js docs: server.listen()](https://nodejs.org/api/net.html#serverlisten)
- [Node.js docs: server.close()](https://nodejs.org/api/http.html#serverclosecallback)
