Last updated: Apr 5, 2024
Reading timeยท6 min
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:
Here is the complete error message.
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 }
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.
npx kill-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.
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
.
# 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.
An alternative way to stop the process that runs on the specific port is to restart your computer.
You can also stop the process that runs on the port with an operating system-specific command.
lsof -i :3000
If you get a permissions error, run the command prefixed with sudo
.
sudo lsof -i :3000
The command shows the PID (process ID) number that we need for the next command.
kill -9 <PID>
by replacing <PID>
with the output you got from
the previous command.kill -9 <PID>
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.
To stop the process that runs on the port on Windows:
Click on the Search bar and type CMD.
Right-click on the Command Prompt application and click "Run as administrator".
taskkill /F /IM node.exe
command.taskkill /F /IM node.exe
Alternatively, you can use the Graphic user interface.
To stop the process:
node.exe
processes, right-click and
click on End task.Try to start your server after stopping the process.
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.
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
.
Alternatively, you can use process.env
to be able to set a custom port when
starting your server.
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.
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.
# ๐๏ธ 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.
node index.js
As the screenshot shows, the server started on port 5432.
You can print the PORT
variable with the following commands.
# ๐๏ธ Linux and macOS echo $PORT # ๐๏ธ on Windows with CMD echo %PORT% # ๐๏ธ on Windows with PowerShell echo $Env:PORT
If you need to remove the PORT
environment variable, use the following
command.
# ๐๏ธ for macOS, Linux or Windows Git Bash unset PORT # ---------------------------------------------------- # ๐๏ธ for Windows CMD (Command Prompt) set PORT= # ---------------------------------------------------- # ๐๏ธ for Windows PowerShell $env:PORT=''
app.listen()
If you have multiple calls to the app.listen()
method in your application, the
error is caused.
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.
Here is an example of changing the port when using the native Node.js http server.
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);
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.
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:
On macOS Ventura:
On some other macOS flavors:
You can learn more about the related topics by checking out the following tutorials: