Error: listen EADDRINUSE: address already in use 3000;

Viewed 12142

I have a .env file in my root directory with PORT = 3000; inside

In my app.js, I am using the .env file to listen to port 3000

require('dotenv').config();

const express = require('express');
const app = express();
const port = process.env.PORT || 4000;

app.get('/', (req, res) => {
  res.send('Hello World!!!');
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

After trying to run the file, I get the following error

Error: listen EADDRINUSE: address already in use 3000;

I'm on a Mac, so I tried using sudo lsof -i :3000 in the terminal and am asked for my password.

I type in my password and hit Enter but nothing happens.

How can I remove the error? I believe my password is correct. I did get a huge Mac OS update today--could that cause some password related issues?

5 Answers

Changing the app.js file to

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

require('dotenv').config();

app.get('/', (req, res) => {
  res.send('Hello World!!!');
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

resolves the issue, but PORT in .env is not being read...

Try this command:

sudo ps -ef | grep 3000

I fixed this error using this command on Windows :

netstat -ano | findstr :3000. 

After entering this command above, you will be given this TCP address:

  TCP    0.0.0.0:3000           0.0.0.0:0              LISTENING       12356
  TCP    [::]:3000              [::]:0                 LISTENING       12356

And then run:

taskkill /PID 12356 /F

eg:

C:\Users\Desktop\nodejs>netstat -ano | findstr :3000

  TCP    0.0.0.0:3000           0.0.0.0:0              LISTENING       12356
  TCP    [::]:3000              [::]:0                 LISTENING       12356

C:\Users\Desktop\nodejs>taskkill /PID 12356 /F

SUCCESS: The process with PID 12356 has been terminated.

you should delete semicolon behind port number

for example PORT=3000

You can simply change port number

from 3000 to 3010 or what

Just stay away form already used port

Related