Error: listen EADDRINUSE: address already in use [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
6 min

banner

# Error: listen EADDRINUSE: address already in use

The "Error: listen EADDRINUSE: address already in use" occurs when the port on which you started your Node.js server is already taken.

There are 2 main ways to solve the error:

  1. Stop the process that runs on the specified port before starting your server.
  2. Change the port on which you start your Node.js server.

error listen eaddrinuse address already in use 3000

Here is the complete error message.

shell
Error: listen EADDRINUSE: address already in use :::3000 Emitted 'error' event on Server instance at: at emitErrorNT (node:net:1744:8) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '::', port: 3000 }

# Stop the process that runs on the specified port

One way to solve the error is to stop the process that runs on the port on which you start your server.

The easiest way to stop the process is to use the kill-port npm package.

The package works on Windows, macOS and Linux.

shell
npx kill-port 3000

stop process on port 3000

Make sure to replace 3000 with the port your server should run on.

You can also use the package to stop multiple ports.

shell
npx kill-port 3000 5000 9000

Try to start your development server after stopping the process that runs on the port.

You can also install the package globally instead of using npx.

shell
# Install the package globally npm install --global kill-port # Stop the process on port 3000 kill-port --port 3000

Try to start your server after stopping the process.

# Restarting your computer

An alternative way to stop the process that runs on the specific port is to restart your computer.

Unless the process starts immediately after your computer boots, restarting your PC will get the job done.

# Using an operating systems-specific command to stop the process

You can also stop the process that runs on the port with an operating system-specific command.

# Stop the process that runs on the port on macOS and Linux

  1. On macOS and Linux, issue the following command to find the PID (process ID).
shell
lsof -i :3000

find pid of process on linux

If you get a permissions error, run the command prefixed with sudo.

shell
sudo lsof -i :3000

The command shows the PID (process ID) number that we need for the next command.

find pid of process on linux

  1. Issue the kill -9 <PID> by replacing <PID> with the output you got from the previous command.
shell
kill -9 <PID>

stop-process-on-linux

Make sure to replace the <PID> placeholder with your specific PID number.

If you get multiple PID numbers when running lsof -i :3000, issue the kill -9 command for each PID.

Try to start your server after stopping the process.

# Stop the process that runs on the port on Windows

To stop the process that runs on the port on Windows:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Issue the taskkill /F /IM node.exe command.
CMD
taskkill /F /IM node.exe

stop process on windows

Alternatively, you can use the Graphic user interface.

To stop the process:

  1. Click on the Start menu and type Task Manager.

search task manager

  1. In the Details tab, select the node.exe processes, right-click and click on End task.

end node task windows

  1. Click on End process to confirm and the process will stop.

Try to start your server after stopping the process.

# Change the PORT your Node.js server runs on

If you have a basic Express.js server, you can change the port in your index.js or app.js file, depending on where you initialize your server.

Here is an example of setting the port in a basic Express.js application.

index.js
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); // ๐Ÿ‘‡๏ธ Specify port number const port = 3456; app.listen(port, () => { console.log(`Example app listening on port ${port} visit: http://localhost:${port}`); });

We pass the port as the first argument to the app.listen() function.

When I start my server with node index.js, it starts on port 3456.

change node port

# Setting a dynamic PORT with an environment variable

Alternatively, you can use process.env to be able to set a custom port when starting your server.

index.js
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); // ๐Ÿ‘‡๏ธ Set custom port if provided const port = process.env.PORT || 3456; app.listen(port, () => { console.log(`Example app listening on port ${port} visit: http://localhost:${port}`); });

The process.env.PORT line checks if a PORT environment variable is defined.

shell
const port = process.env.PORT || 3456;

If the environment variable is defined, it gets used when starting the server, otherwise, the server starts on port 3456.

Now you can set the environment variable before running the node index.js command.

shell
# ๐Ÿ‘‡๏ธ for macOS, Linux or Windows Git Bash export PORT=5432 # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows CMD (Command Prompt) set PORT=5432 # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows PowerShell $env:PORT=5432

After you set the environment variable, issue the node index.js command.

shell
node index.js

change port based on environment variable

As the screenshot shows, the server started on port 5432.

You can print the PORT variable with the following commands.

shell
# ๐Ÿ‘‡๏ธ Linux and macOS echo $PORT # ๐Ÿ‘‡๏ธ on Windows with CMD echo %PORT% # ๐Ÿ‘‡๏ธ on Windows with PowerShell echo $Env:PORT

print port number

If you need to remove the PORT environment variable, use the following command.

shell
# ๐Ÿ‘‡๏ธ for macOS, Linux or Windows Git Bash unset PORT # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows CMD (Command Prompt) set PORT= # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows PowerShell $env:PORT=''

remove port environment variable

# Make sure you don't have multiple calls to app.listen()

If you have multiple calls to the app.listen() method in your application, the error is caused.

index.js
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); const port = process.env.PORT || 3456; // ๐Ÿ‘‡๏ธ First call to app.listen app.listen(port, () => { console.log(`Example app listening on port ${port} visit: http://localhost:${port}`); }); // ๐Ÿ‘‡๏ธ Second call to app.listen app.listen(port, () => { console.log(`Example app listening on port ${port} visit: http://localhost:${port}`); });

The code sample causes the error because we called app.listen() twice.

To solve the error, remove the second call to the app.listen() method.

# Changing the PORT when using the native HTTP Node.js server

Here is an example of changing the port when using the native Node.js http server.

index.js
import http from 'http'; const server = http.createServer((request, response) => { response.writeHead(200, { 'Content-Type': 'text/plain', }); response.write('Hello, World!\n'); response.end(); }); const PORT = 3456; server.on('listening', function () { console.log(`Server listening on port ${PORT} Visit: http://localhost:${PORT}`); }); server.listen(PORT);

change nodejs native server port

If I start my server with node index.js, it starts on port 3456 as shown in the screenshot.

The port number is passed to the server.listen() method.

# Disabling AirPlay on Mac

In recent versions, AirPlay on macOS uses ports 5000 and 7000 by default

If you need to run your server on the specified ports, you have to disable AirPlay.

On macOS Monterey, you can disable AirPlay by:

  • System Preferences -> Sharing -> untick AirPlay Receiver

On macOS Ventura:

  • System Settings -> General -> disable AirPlay Receiver

On some other macOS flavors:

  • System Settings -> Choose the Airplay extended or mirrored display -> Choose Off

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev