Error: listen EADDRINUSE :::9000, killall && kill -9 are not working [duplicate] - node.js

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?

If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

Related

Port is already in use - for any port on Mac

So recently I got a Macbook and I have cloned my project on it which is a MERN stack application. The port in my .env is 5000. When I attempt to start up the server I get the error that the port is already in use.
I figured "hmm ok, i'll change it to 5100". That worked, but only for that time, the next time I tried to run it i got the same error for port 5100.
Anyone have any idea what's going on?
Check the process Id
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
Check the process name
ps -Ao user,pid,command | grep -v grep | grep <<pid>>
which will output something similar to this, which means your mac OS is utilising port 5000
622 /System/Library/CoreServices/ControlCenter.app/Contents/MacOS/ControlCenter
This is because new AirPlay functionality in mac Monterey version is utilising port 5000. To disable that you can
go to system preferences -> search for sharing -> untick AirPlay Receiver in left side panel
You could use
netstat -anvp tcp | awk 'NR<3 || /LISTEN/'
(credits: https://apple.stackexchange.com/a/117648)
and use
ps aux PID
to see the correspoding proccess. (PID is number from PID columen, ofc.)
This is due to the new AirPlay functionality on Monterrey Os.
Control Center stops listening to those ports when you turn off “AirPlay Receiver”.
In the “Sharing” System Preference, so uncheck airplay receiver:
After that you must kill the process that take the 500 port
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
That show you which process to kill
Then, you can use the port 5000 again.

Finding TCP connections with pids

I tried to display all the TCP connections with pids on ubuntu terminal but am not able to do it. Can someone tell a single command to find all tcp connections with pids on ubuntu terminal ?
Take a look at lsof man page. I found:
[sudo] lsof -i TCP

How to find out which port number a process is using and the process using a specific port number

I'm beginner with openhab, after launching the server /etc/init.d/openhab2 start i got that [ ok ] Starting openhab2 (via systemctl): openhab2.service, but I still have no access to the platform via http://localhost:8080/
I want to know :
which process is using port 8080
which port openhab runs on
thanks
you can use lsof,
lsof | grep TCP | grep 8080 and lsof | grep openhab | grep TCP
if a process forks and the child process is the one that is using a port it may not work as expected

How can I find a process using a TCP port?

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?
If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

how to release localhost from Error: listen EADDRINUSE

i am testing a server written in nodejs on windows 7
and when i try to run the tester in the command line i get the following error
Error: listen EADDRINUSE
at errnoException (net.js:614:11)
at Array.0 (net.js:704:26)
at EventEmitter._tickCallback (node.js:192:40)
how can I fix it without rebooting?
Run:
ps -ax | grep node
You'll get something like:
60778 ?? 0:00.62 /usr/local/bin/node abc.js
Then do:
kill -9 60778
It means the address you are trying to bind the server to is in use. Try another port or close the program using that port.
On Linux (Ubuntu derivatives at least)
killall node
is easier than this form.
ps | grep <something>
kill <somepid>
Neither will work if you have a orphaned child holding the port. Instead, do this:
netstat -punta | grep <port>
If the port is being held you'll see something like this:
tcp 0 0.0.0.0:<port> 0.0.0.* LISTEN <pid>/<parent>
Now kill by pid:
kill -9 <pid>
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>
You are getting the error EADDRINUSE because the port, which your application wants to use, is occupied by another process. To release this port, you need to terminate the occupying process.
Since you are on Windows, you can terminate the process using the command prompt (cmd). With the cmd you can discover the process ID (PID) of the blocking application. You will need the PID in order to terminate / kill the process.
Here is a step-by-step guide...
Find all processes which are running on a specified port (in this example, Port is "3000"):
netstat -ano | find ":3000 "
The netstat command will list up all processes running on the specified port. In the last column of the netstat results you can see the PIDs (in this example, PID is "8308"). You can find out more about a specific PID by running the following command:
tasklist /fi "pid eq 8308"
If you want to terminate a process, you can do that with the following command by using its PID (in this example, PID is "8308"):
taskkill /pid 8308 /f
Screenshot
When you get an error Error: listen EADDRINUSE,
Try running the following shell commands:
netstat -a -o | grep 8080
taskkill /F /PID 6204
I greped for 8080, because I know my server is running on port 8080. (static tells me when I start it: 'serving "." at http://127.0.0.1:8080'.) You might have to search for a different port.
suppose your server is running on port 3000
lsof -i tcp:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 11716 arun 11u IPv6 159175 0t0 TCP *:3000 (LISTEN)
after that use kill -9 <pid>
in the above case sudo kill -9 11716
use below command to kill a process running at a certain port - 3000 in this example below
kill -9 $(lsof -t -i:3000)
One possible solution that worked for me was simply to close the window in browser where I had the corresponding "http://localhost:3000/" script running.
When you get an error
Error: listen EADDRINUSE
Open command prompt and type the following instructions:
netstat -a -o | grep 8080
taskkill /F /PID** <*ur Process ID no*>
after that restart phone gap interface.
If you want to know which process ID phonegap is using, open TASK MANAGER and look at the Column heading PID and find the PID no.
Check the Process ID
sudo lsof -i:8081
Than kill the particular Process
sudo kill -9 2925
The aforementioned killall -9 node, suggested by Patrick works as expected and solves the problem but you may want to read the edit part of this very answer about why kill -9 may not be the best way to do it.
On top of that you might want to target a single process rather than blindly killing all active processes.
In that case, first get the process ID (PID) of the process running on that port (say 8888):
lsof -i tcp:8888
This will return something like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 57385 You 11u IPv6 0xac745b2749fd2be3 0t0 TCP *:ddi-tcp-1
(LISTEN)
Then just do (ps - actually do not. Please keep reading below):
kill -9 57385
This works on Mac:
Step 1.
sudo lsof -i tcp:3000 (or whatever port you want to kill)
Above command will give you the Process Id(s) currently holding the port.
Step 2.
Kill -9 <pid>
you can change your port in app.js or in ur project configuration file.
default('port', 80)
and to see if port 80 is already in use you can do
netstat -antp |grep 80
netstat -antp |grep node
you might wanna see if node process is already running or not.
ps -ef |grep node
and if you find its already running, you can kill it using
killall node
I created 2 servers, listening on same port 8081, running from same code, while learning
1st server creation shud have worked
2nd server creation failed with EADDRINUSE
node.js callback delays might be reason behind neither worked, or 2nd server creation had exception, and program exited, so 1st server is also closed
2 server issue hint, I got from:
How to fix Error: listen EADDRINUSE while using nodejs?
Error: listen EADDRINUSE to solve it in Ubuntu run in terminal netstat -nptl and after this kill -9 {process-number} this command is to kill node process and now you can try to run again node server.js command
Ex
listen EADDRINUSE :::8080
netstat -nptl
tcp6 0 0 :::9000 :::* LISTEN 9660/java
tcp6 0 0 :::5800 :::* LISTEN -
tcp6 0 0 :::5900 :::* LISTEN -
tcp6 0 0 :::8080 :::* LISTEN 10401/node
tcp6 0 0 :::20080 :::* LISTEN 9660/java
tcp6 0 0 :::80 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 :::10137 :::* LISTEN 9660/java
kill -9 10401
I have node red installed on my Mac and my Raspberry Pi. Had the exact same problem and doing 'killall' didn't help. Once I shut down the Raspberry Pi and restarted my computer it worked fine.
It may be possible that you have tried to run the npm start command in two different tabs .you cant run npm start when it is already running in some tab.check it once .
To anyone who has tried all of the above, but still can't find the rouge process, try them all again but make sure you include "sudo" in front of the commands.
If you like UI more, find the process Node.js in windows task manager and kill it.
ps -ef |grep node
find app.js ,
kill pid of app.js
simply kill the node
as
pkill -9 node
in terminal of ubantu
than start node
You will need to kill the port by trying to use the following command on the terminal
$ sudo killall -9 nodejs
I have solved this issue by adding below in my package.json for killing active PORT - 4000 (in my case) Running on WSL2/Linux/Mac
"scripts": {
"dev": "nodemon app.js",
"predev":"fuser -k 4000/tcp && echo 'Terminated' || echo 'Nothing was running on the PORT'",
}
Source
I Just delete that (cmd) Terminal and open another terminal and run again
node myfile.js
it works awesomely for me.
It's might be too late but it's working like a charm.
You need pm2 to be installed
npm install -g pm2
To stoping the current running server (server.js):
pm2 stop -f server.js
It's year 2022 now and I am on Monterey (Mac user).
lsof -i tcp:3000
Kill -9 <PID shown in your list>
To kill node server first run this command in your terminal :
top
open another window then copy the server id from the previous window: PID number -9 kill
so now you killed your node server try again to run your app.
I used the command netstat -ano | grep "portnumber" in order to list out the port number/PID for that process.
Then, you can use taskkill -f //pid 111111 to kill the process, last value being the pid you find from the first command.
One problem I run into at times is node respawning even after killing the process, so I have to use the good old task manager to manually kill the node process.
In window, please execute this command:
taskkill /F /PID 1952

Resources