ChatGPT解决这个技术问题 Extra ChatGPT

Node / Express: EADDRINUSE, Address already in use - Kill server

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 handlers, but thats the basic idea. The problem 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 ctr + 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.

Actually, instead of Ctrl + z you should use Ctrl + c which will close the program correctly by sending SIGQUIT :) See the wiki for further details :)
You mean SIGINT. SIGQUIT is due to `ctrl + \`
Try pkill nodejs or pkill node if on UNIX-like OS
I had a similar issue and found this package that will allow you to exit cleanly when you CTRL+C: npmjs.com/package/exit-hook
restarting your computer is a terrible solution.

d
djburdick

You can also go the command line route:

ps aux | grep node

to get the process ids.

Then:

kill -9 PID

Doing the -9 on kill sends a SIGKILL (instead of a SIGTERM). SIGTERM has been ignored by node for me sometimes.


ps aux | grep node shows nothing; still textareaserver --editor-cmd='gvim -f' fails: 14 Mar 21:19:30 - socket.io ready - accepting connections Could now start the server: EADDRINUSE, Address already in use
Why this rather than killall -9 node
I used this answer for a long time, then one day composed it into a one liner for convenience.. this command will kill any process running on a given port (8000 in this example): lsof -n -i4TCP:8000 | grep LISTEN | tr -s ' ' | cut -f 2 -d ' ' | xargs kill -9
I had multiple node servers running at once, some of them Electron apps. I had to use just kill with the specific process id instead of killall.
Just as a PSA: be very careful with what you kill since many services use Electron and you might inadvertently kill an unrelated process.
M
Mina Gabriel

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}

This command clearly identifies the PID unlike the output of ps aux | grep node for me. I also did not need sudo
This is the best answer. Worked fine for me with port 8081, using React Native with yarn.
This worked great for me. I could identify where the port 3000 was used and so close it. I also did not need sudo.
Very well. I also tried after getting the command killall -9 {COMMAND} e.g. killall -9 node
The command to find a process using a specific port is very useful. So glad you posted this.
r
rsjaffe

I hit this on my laptop running win8. this worked.

Run cmd.exe as 'Administrator':

C:\Windows\System32>taskkill /F /IM node.exe
SUCCESS: The process "node.exe" with PID 11008 has been terminated.

I was running it from the Command Window and closed it by accident. Node kept running in the back ground... (even after the session was terminated). In my case once I closed the browser tab that was connected to it via web-sockets it finally terminated.
taskkill /F /IM node.exe works like a charm for me on Windows from any directory :-) Thanks for the share !!
This is the only single-line working solution I've been able to verify for windows
Works on windows 10. I didn't have to run the cmd.exe as admin btw.
This is the best answer. The others gave me "command could not be recognized". This just works.
T
Tor Valamo

process.on('exit', ..) isn't called if the process crashes or is killed. It is only called when the event loop ends, and since server.close() sort of ends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event...

On crash, do process.on('uncaughtException', ..) and on kill do process.on('SIGTERM', ..)

That being said, SIGTERM (default kill signal) lets the app clean up, while SIGKILL (immediate termination) won't let the app do anything.


it's also useful hook on process.on('SIGINT', ... )
A
Ayan

Check the PID i.e. id of process running on port 3000 with below command :

lsof -i tcp:3000

It would output something like following:

COMMAND  PID   USER   FD   TYPE  DEVICE  SIZE/OFF NODE NAME
node     5805  xyz    12u  IPv6  63135    0t0     TCP  *:3000 (LISTEN)

Now kill the process using :

kill -9 5805

I get no output if I use lost. Does this mean there is no process on that part?
Don't use kill -9 unless you know what you are doing. Basically, try kill PID and kill -2 PID and kill -15 PID and wait a while, and if the process is still around, then bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter.
I get no output with lsof... but the issue persists.
B
Balaji Rajendran

I found this solution, try it Give permission use sudo

  sudo pkill node

I still see node process with this command: ps aux | grep node
this worked for me
this worked for me
F
Fatih Bulut

For macOS Monterey(12.0) and above users.

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


this worked for me
Great find! thanks
This worked for me as well. Thank you
This was the issue here - how on earth did you figure this out :D
Worked for me as well !
D
Dharman

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.


easy to remember than other answers. Thanks :)
C
Cody Gray

Linux

Run ps and determine the PID of your node process.

Then, run sudo kill PID

Windows

Use tasklist to display the list of running processes:

tasklist /O

Then, kill the node process like so (using the PID obtained from the tasklist command):

taskkill /pid PID

And if on Windows?
Just as a note... for some reason I had to use double slashes on my flags for the taskkill command from git-bash: taskkill //IM node.exe and i just killed node. worked.
there is no /O argument in the documentation docs.microsoft.com/en-us/windows-server/administration/…
f
floribon

Here is a one liner (replace 3000 with a port or a config variable):

kill $(lsof -t -i:3000)

S
Shahdat

For windows open Task Manager and find node.exe processes. Kill all of them with End Task.

https://i.stack.imgur.com/r29OI.png


Update: End Task for "Node.js JavaScript Runtime" is sufficient
It's has been 11 years and still helpful, this is the best answer for windows.
m
malik kurosaki

I usually use

npx kill-port 3000

or on my mac.

killall node

M
MD SHAYON

In Linux try

pkill nodejs 
//or 
pkill node

N.B. This will kill all process

to delete some specific port

// This will show all the port number which node js is using
netstat -lntp | grep node
// Kill your targeted used port
kill -HUP process_id 

so this will close express only or all running node processes like another app on react for example?
K
Kit

I was getting this error once and took many of the approaches here.

My issues was that I had two app.listen(3000); calls in the same app.js script. The first app.listen() succeeded where the second threw the error.

Another useful command I came across that helped me debug was sudo fuser -k 3000/tcp which will kill any rogue processes you might have started (some processes may restart, e.g. if run with forever.js, but it was useful for me).


the same issue here... weird that it was properly working in debugging while getting this error for running npm start
j
jmrueda

For Visual Studio Noobs like me

You may be running the process in other terminals!

After closing the terminal in Visual Studio, the terminal just disappears.

I manually created a new one thinking that the previous one was destroyed. In reality, every time I was clicking on New Terminal I was actually creating a new one on top of the previous ones.

So I located the first terminal and... Voila, I was running the server there.

https://i.stack.imgur.com/WOWn8.png


E
Ezra Siton

Windows by Cmd

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

https://i.stack.imgur.com/oDrDs.png

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

https://i.stack.imgur.com/XzDgA.png

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

https://i.stack.imgur.com/1QCW8.png

And end task

https://i.stack.imgur.com/ntk6c.png

Visual studio

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

https://i.stack.imgur.com/FtFmr.png


Wonderful share, thanks
A
Adam Gerard

You may run into scenarios where even killing the thread or process won't actually terminate the app (this happens for me on Linux and Windows every once in a while). Sometimes you might already have an instance running that you didn't close.

As a result of those kinds of circumstances, I prefer to add to my package.json:

"scripts": {
    "stop-win": "Taskkill /IM node.exe /F",
    "stop-linux": "killall node"
},

I can then call them using:

npm run stop-win
npm run stop-Linux

You can get fancier and make those BIN commands with an argument flag if you want. You can also add those as commands to be executed within a try-catch clause.


A
Asclepius

FYI, you can kill the process in one command sudo fuser -k 3000/tcp. This can be done for all other ports like 8000, 8080 or 9000 which are commonly used for development.


D
Daniyal
ps aux | grep node
kill -9 [PID] (provided by above command)

Description:

ps will give the process status, aux provide the list of a: all users processes, u: user own processes, x: all other processes not attached to terminal. pipe symbol: | will pass the result of ps aux to manipulate further. grep will search the string provided(node in our case) from the list provided by ps aux.


Don't use kill -9 unless you know what you are doing. Basically, try kill PID and kill -2 PID and kill -15 PID and wait a while, and if the process is still around, then bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter.
r
raison

First find out what is running using:

sudo lsof -nP -i4TCP:3000 | grep LISTEN

You will get something like:

php-fpm 110 root    6u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)
php-fpm 274 _www    0u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)
php-fpm 275 _www    0u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)

Then you can kill the process as followed:

sudo kill 110

Then you will be able to run without getting the listen EADDRINUSE :::3000 errors


I am using nodemon and it restarts the service again. Is their any way to kill nodemon?
v
var foobar

PowerShell users:

Taskkill /IM node.exe /F


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

bash$ kill 31157

S
SendETHToThisAddress

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.


Absolutely perfect, thanks dude.
i
idanuda

Task Manager (ctrl+alt+del) ->

Processes tab ->

select the "node.exe" process and hit "End Process"


why kill the entire node process if what am looking is to kill a node port process .
That doesn't make sense, there is no separate process for holding the port. If you have several node processes accessing different ports, or none at all, you have to single out the one which is holding on to the port; but it will still be just a node process.
A
Arun Killu

Just in case check if you have added this line multiple times by mistake

app.listen(3000, function() {
  console.log('listening on 3000')
});

The above code is for express but just check if you are trying to use the same port twice in your code.


N
Najathi

In windows users: open task manager and end task the nodejs.exe file, It works fine.


R
Raghav

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:

https://i.stack.imgur.com/q66X2.png

killed it via:

taskkill /pid 43144 /f

https://i.stack.imgur.com/15Qpc.png

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


S
Salah Ben Bouzid

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 

A
Amal Augustine Jose

With due respect to all the answers in the form, I would like to add a point.

I found that when I terminate a node app on error using Ctrl + Z, the very next time when I try to open it got the same error EADDRINUSE.

When I use Ctrl + C to terminate a node app, the next time I opened it, it did without a hitch.

Changing the port number to something other than the one in error solved the issue.


Ctrl + C is the correct. I used the same port number as before and it worked as before.
<key>Ctrl-Z</key> doesn't stop the process. It puts it in the background so you can run other commands. This is a Unix shell thing. To continue the process use fg in that same console. You can then see what's happening in that server after having typed various commands in the command line.
t
touchStone

You may use hot-node to prevent your server from crashing/ run-time-errors. Hot-node automatically restarts the nodejs application for you whenever there is a change in the node program[source] / process[running node program].

Install hot-node using npm using the global option:

npm install -g hotnode


pm2 is a better choice. more robust, more options. and doesn't have the problem when running as root that forever has.
@Lucas What is this root problem in forever that you speak of? I am unfortunately forced to use forever instead of pm2 on a product at work (due to some licensing crap), and this worries me a lot!