Skawful
Skawful

Reputation: 9917

Node / Express: EADDRINUSE, Address already in use - how can I stop the process using the port?

I have a simple server running in node.js using connect:

var server = require('connect').createServer();
//actions...
server.listen(3000);

In my code I have actual route handlers, but that's the basic idea. The error I keep getting is:

EADDRINUSE, Address already in use

I receive this error when running my application again after it previously crashed or errors. Since I am not opening a new instance of terminal I close out the process with ctrl + z.

I am fairly certain all I have to do is close out the server or connection. I tried calling server.close() in process.on('exit', ...); with no luck.

Upvotes: 910

Views: 1766213

Answers (30)

Vivek
Vivek

Reputation: 81

The Machine still understands that all requests to the port is coming from different origins.

Solution: Restart your computer

Upvotes: 0

Mian Aamir Shehzad
Mian Aamir Shehzad

Reputation: 228

I'm using Windows 10 and I'm on the development of React Native project when I encountered this problem.

To solve this,

Type

npx kill-port portName

like in my case, it was

npx kill-port 8081

, and hit enter.

After that, run this command to check whether your port is empty or not.

adb reverse tcp:8081 tcp:8081

If port 8081 is still busy, it will pop-out to the terminal, otherwise, you're good to go to run npm start

Upvotes: 7

tom
tom

Reputation: 10601

It is always worth executing a shutdown method at the end, where process.exit() is executed to terminate the app properly and unbind the port.

In this context, process.stdin.resume() is necessary to also catch user events like CTRL+C. Therefore, it is probably the case that your server.close() command is not executed (as described in this answer https://stackoverflow.com/a/4086015/10944219)

As an example of how this could look in practice:

// interrupt from keyboard (for example: ctrl+c event)
process.on('SIGINT', (signal) => { 
  shutdown({ type: 'SIGINT', signal });
});

// termination request (for example: task scheduler ?)
process.on('SIGTERM', (signal) => { 
  shutdown({ type: 'SIGTERM', signal });
});

// uncaught exceptions
process.on('uncaughtException', (err, origin) => { 
  shutdown({ type: 'uncaughtException', err, origin });
});

// program will not close instantly, is wating for user input
// (this is important to catch SIGINT/SIGTERM)
process.stdin.resume();

const shutdown = (obj) => {
  console.log(obj);

  // do stuff ...
  // ... clean up things
  // ... write some logs
  // ... send mail to admin

  // exit condition if reasonable
  // (for example: termination by user, but not by uncaughtException)
  if (obj.type === 'SIGINT')
    process.exit();
};

If the error EADDRINUSE, Address already in use still occurs, you can terminate all Node processes under Windows with the following command:

taskkill /F /IM node.exe

Upvotes: 1

Chamudi Abeysinghe
Chamudi Abeysinghe

Reputation: 400

You can check which process is using port 8081 by running the following command in your terminal:

Run: lsof -i :8081

And then I used:

Run: pkill -f "react-native"

After ensuring port 8081 is free, try starting the React Native server again.

Run: npm start

Upvotes: 2

Hender
Hender

Reputation: 400

In linux and Mac

If you are in the same terminal session, just type: fg, which stands for foreground, in order to comeback to foreground again the app instance and simple kill it as usual.

Upvotes: -1

Mohammad Zourmand
Mohammad Zourmand

Reputation: 45

Maybe another your project scripts is running !

Make sure no other scripts are running, such as "start" !!!

Upvotes: 1

For Linux/ubuntu

npx kill-port 3000 or

npx kill-port 4000 or

npx kill-port 8000 

Whichever post you are using

Like

app.listen(4000, () => {
  console.log('Listening on 4000');
});

Upvotes: 0

Mina Gabriel
Mina Gabriel

Reputation: 25150

First, you would want to know which process is using port 3000

sudo lsof -i :3000

this will list all PID listening on this port, once you have the PID you can terminate it with the following:

kill -9 <PID>

where you replace <PID> by the process ID, or the list of process IDs, the previous command output.

Upvotes: 987

Anroche
Anroche

Reputation: 1021

An easy way is to list all the processes that are listening on your port

lsof -i tcp:YOUR_PORT | grep LISTEN

Then you can kill them :

kill -9 {PID}

Upvotes: 0

Harkirat Singh
Harkirat Singh

Reputation: 55

Try to changing port number. It worked for me. For example you can change the port number from 3000 to 3001.

Upvotes: 1

Fatih Bulut
Fatih Bulut

Reputation: 2657

For macOS Monterey(12.0):

Apple introduced some changes for AirPlay on macOS Monterey. Now, it uses 5000 and 7000 ports. If you are using these ports in your project, you need to disable this feature.

System Preferences > Sharing > untick AirPlay Receiver


For macOS Ventura(13.0) and above users:

System Settings > General > disable AirPlay Receiver

Upvotes: 126

If you are a windows user just go to task manager and end all the task of node jsenter image description here

Upvotes: 7

Mrk Fldig
Mrk Fldig

Reputation: 4486

Really simply for all OS's ..

npx kill-port 3000

Although your problem is as mentioned above you need to catch the different ways node can exit for example

process.on('uncaughtException', (err, origin) => {
    console.log(err);
});

// insert other handlers.

Upvotes: 23

mikemaccana
mikemaccana

Reputation: 123500

Modern Windows (powershell)

Current versions of Windows default to using Powershell rather than cmd. Here you'd run:

Get-NetTCPConnection | where Localport -eq 3000 | select Localport,OwningProcess

to find out what the app is.

Upvotes: 4

confused_
confused_

Reputation: 1691

In my case, this problem was caused by the NodeJS environment

Two solutions :

  1. Open Taskmanager and Kill all the Nodejs processes (Name: Node.js JavaScript Runtime)

  2. Try changing the port of your server from 3000 to some other port (say: 3111) that is not currently being used.

Upvotes: 3

Black
Black

Reputation: 20342

This happend to me, because I had multiple instances of visual studio open in my ubuntu without noticing it and there was xdebug already running in one of them.

Upvotes: -2

malik kurosaki
malik kurosaki

Reputation: 2042

I usually use

npx kill-port 3000

or on my mac.

killall node

Upvotes: 95

Salah Ben Bouzid
Salah Ben Bouzid

Reputation: 411

using netstat to get all node processes with the port they are using and then kill the only one you want by PID

netstat -lntp | grep node

you will get all node processes

tcp6  0      0 :::5744    :::*    LISTEN     3864/node

and then when you get the PID (3864) just kill the processes by PID

  kill -HUP PID 

Upvotes: 5

Mohammad Zeeshan
Mohammad Zeeshan

Reputation: 4731

Simple exit from the server and change the server port 3000 to 31000 and its working fine.

Upvotes: 1

Mehul
Mehul

Reputation: 112

I had another problem. I had declared the port twice. Don't do that mistake as well.

app.listen(port, function () {
  console.log('Example app listening on port')
})

app.listen(3000, function() {
  console.log("Server started on port 3000");
});

Instead do this:

const port = 3000;

app.listen(port, function () {
  console.log('Example app listening on port')
})

Upvotes: 0

Ezra Siton
Ezra Siton

Reputation: 7781

Windows by Cmd

1/2. search => write cmd => open node.js command prompt

enter image description here


2/2. Run windows command: taskkill

Ends one or more tasks or processes.

taskkill /f /im node.exe

/f - force ended

/im - Specifies the image name of the process to be terminated.

node.exe - executable file

enter image description here

Windows - Mannualy by Task Manager

This command is the same as going to Task Manager under the details tab & select node tasks (Tidy in my opinion).

enter image description here

And end task

enter image description here

Visual studio

Sometimes there is more than one terminal/task (client/server and so on). Select and close by ctrl + c.

enter image description here

Upvotes: 24

Raghav
Raghav

Reputation: 9628

On Windows, I was getting the following error:

EADDRINUSE: address already in use :::8081.

Followed these steps:

  • Opened CMD as Admin
  • Ran the folowing

command netstat -ano|findstr "PID :8081"

got the following processes:

enter image description here

killed it via:

taskkill /pid 43144 /f

enter image description here

On MAC you can do like this:

raghavkhunger@MacBook-Air ~ % lsof -i tcp:8081 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 23722 username 24u IPv6 0xeed16d7ccfdd347 0t0 TCP *:sunproxyadmin (LISTEN)

username@MacBook-Air ~ % kill -9 23722

Upvotes: 9

Yanov
Yanov

Reputation: 673

bash$ sudo netstat -ltnp | grep -w ':3000'
 - tcp6    0      0 :::4000      :::*        LISTEN      31157/node      

bash$ kill 31157

Upvotes: 15

Deepam Gupta
Deepam Gupta

Reputation: 2712

Rewriting @Gerard 's comment in my answer:

Try pkill nodejs or pkill node if on UNIX-like OS.

This will kill the process running the node server running on any port. Worked for me.

Upvotes: 47

agnes_st
agnes_st

Reputation: 57

I was using debugger and just not stopped the processes running with Ctrl+C. So when I wanted to start debugging I got this error.

Upvotes: -2

vedant mehta
vedant mehta

Reputation: 21

I was facing the same problem. I just changed my port number from 8000 to 6000. as you have 3000 you try 5000,4000,7000,8000 etc.

Upvotes: 0

Srinivasan Kasiram
Srinivasan Kasiram

Reputation: 94

delete undefined file in your project root directory (which created on app crash)

Upvotes: -1

Yogesh Nogia
Yogesh Nogia

Reputation: 1135

I had the same problem and I found out that it was the nodemon problem. First I was using this script to start my process:

{"dev": "nodemon -r dotenv/config app.js"}

the app boots correctly, but as soon as any file changes, nodemon can't restart it. In the meantime, the app still continues to run in the background. If I do Ctrl+C, it quits, but there's no more process on port 3000, so killing it by port fuser -k 3000/tcp doesn't do anything.

And, I was using .env port in app.js file.

const port = process.env.PORT || 3000;

So, I changed the port value to 3000 only and it worked.

const port = 3000;

I had to find another way to load .env file, but this solved the issue for me. Hope that helps.

Upvotes: 0

SendETHToThisAddress
SendETHToThisAddress

Reputation: 3744

UI solution For Windows users: I found that the top answers did not work for me, they seemed to be commands for Mac or Linux users. I found a simple solution that didn't require any commands to remember: open Task Manager (ctrl+shift+esc). Look at background processes running. Find anything Node.js and end the task.

After I did this the issue went away for me. As stated in other answers it's background processes that are still running because an error was previously encountered and the regular exit/clean up functions didn't get called, so one way to kill them is to find the process in Task Manager and kill it there. If you ran the process from a terminal/powerShell you can usually use ctrl+c to kill it.

Upvotes: 13

Ruchir
Ruchir

Reputation: 1068

Use the below command in the terminal/cmd to change the port(npm run dev is for node.js) you may have other commands to run your app most of them will work while changing the port, easier and faster. Furthermore, you can use any port number that is free in your system instead of 3002

PORT=3002 npm run dev

Most of the times when one runs the project while exiting one abruptly or unknowingly presses control + z that gives you exit out of the port always go for control + c that won't exit from port to run the server or project.

Furthermore, its time to change the port number in your code

server.listen(3002);

Upvotes: 2

Related Questions