Port isn't closing when I Exit out of a server - node.js

Hello everyone So I have this annoying problem where my port isn't closing. For example I'm using an express generator to give me an outline/skeleton of a node js/express server.
Port 3000 is already in use
Usually I would just able to CTRL + Z and it would exit out the server.
But recently it wasn't exiting out of the server completely.
I found a little work around which was this
lsof -wni tcp:3000
It would then generate something like this
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 5623 viet 12u IPv6 59797 0t0 TCP *:3000 (LISTEN)
I would locate the PID and then kill it with this command
kill -9 5623
But now it just becomes quite annoying doing this over and over everytime I exit out of a server. Does anyone know why its doing this or I guess why CTRL Z wasn't working how It used to.

Ctrl+Z in Unix-based operating systems just suspends the application.
If you do
ps aux|grep node
and then
kill -9 processid
you should be able to reclaim the port.
Going forward, Ctrl+C to shut down the application.

try this
sudo fuser -k 3000/tcp

Related

the same process PID

I am really confusing. I've tried to start node server and after executing lsof -i:5000 I have the following.
enter image description here
That point on same PID and kill -9 1043 don't work. Pls help
The same process listens on ipv4 and ipv6 interfaces (same port 5000). It is ok.
Either it is restarted each time you kill it (PID will change in this case),
or you don't have permissions to kill processes of the user (should get "Operation not permitted")

Nodemon Error: listen EADDRINUSE: address already in use :::3000 [duplicate]

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.
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.
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.
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.
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.
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
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
I found this solution, try it
Give permission use sudo
sudo pkill node
I usually use
npx kill-port 3000
or on my mac.
killall node
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.
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
Here is a one liner (replace 3000 with a port or a config variable):
kill $(lsof -t -i:3000)
For windows open Task Manager and find node.exe processes. Kill all of them with End Task.
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).
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.
Windows by Cmd
1/2. search => write cmd => open node.js command prompt
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
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).
And end task
Visual studio
Sometimes there is more than one terminal/task (client/server and so on).
Select and close by ctrl + c.
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.
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.
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.
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
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.
bash$ sudo netstat -ltnp | grep -w ':3000'
- tcp6 0 0 :::4000 :::* LISTEN 31157/node
bash$ kill 31157
PowerShell users:
Taskkill /IM node.exe /F
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.
Task Manager (ctrl+alt+del) ->
Processes tab ->
select the "node.exe" process and hit "End Process"
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.
In windows users: open task manager and end task the nodejs.exe file, It works fine.
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:
killed it via:
taskkill /pid 43144 /f
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
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.
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
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

Killing Stanford core nlp process

I launch Stanford Core NLP server using the following command (on Ubuntu 16.04):
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
I would like to kill this server once I am done using it. Simply closing terminal does not help. It does not release memory. Is there way to kill it and release memory without rebooting computer?
You can always CTRL-C in the terminal window to stop the server.
You could also ps aux | grep StanfordCoreNLPServer to find the pid and then kill the process manually.
When the server is started it should create a shutdown key and you can send that message to the server to close the server. This isn't working on my Macbook Pro (maybe a permission issue ??) but I've seen it work on other machines.
Here is the command:
wget "localhost:9000/shutdown?key=`cat /tmp/corenlp.shutdown`" -O -
Note the shutdown key is stored at /tmp/corenlp.shutdown
If you use the the -server_id server0 option the shutdown key will be stored at this path /tmp/corenlp.shutdown.server0
If you just want to kill the process. You can use lsof command.
#install lsof if missing
sudo apt install lsof
You can find the pid of CoreNLP using
lsof -i:9000
Replace 9000 with the port you used to run the server.
The output looks like
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 15867 XXXX XXX IPv6 XXXXXX 0t0 TCP *:9000 (LISTEN)
Use the pid from here and run.
kill 15867
PID of my server process is 15867.

Can't find out where does a node.js app running and can't kill it

What I did:
I have just set up node environment, installed express, create and installed an express project
express hello
cd hello && npm install
then started the app with "node app".
Environment:
yole#Yole:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.2 (wheezy)
Release: 7.2
Codename: wheezy
yole#Yole:~$ node --version
v0.10.22
yole#Yole:~$ express --version
3.4.4
Problem
When I want to stop this app, I used CTRL+C, but the thing I found is it did not stopped. Then I restarted the server! I found I can still access the page in browser. Orz.
I have tried the following thing but still can't find out the running process.
yole#Yole:~$ killall node
node: no process found
yole#Yole:~$ ps -ef|grep node
yole 3161 2888 0 16:57 pts/1 00:00:00 grep node
yole#Yole:~$ netstat -apn|grep 3000
Question
How to find out the running node process or how to kill it.
=====
update
It is very strange that all browses in my machine can visit the site while it's not available on other machine! I only visit the page with Chrome before I stop the application. It seems to be a cache problem, but how cache shared among browsers..
List node process:
$ ps -e|grep node
Kill the process using
$kill -9 XXXX
Here XXXX is the process number
If you want know, the how may nodejs processes running then you can use this command
ps -aef | grep node
So it will give list of nodejs process with it's project name. It will be helpful when you are running multipe nodejs application & you want kill specific process for the specific project.
Above command will give output like
XXX 12886 1741 1 12:36 ? 00:00:05 /home/username/.nvm/versions/node/v9.2.0/bin/node --inspect-brk=43443 /node application running path.
So to kill you can use following command
kill -9 12886
So it will kill the spcefic node process
You can kill all node processes using pkill node
or you can do a ps T to see all processes on this terminal
then you can kill a specific process ID doing a kill [processID] example: kill 24491
Additionally, you can do a ps -help to see all the available options
Coming to this from a macOS - investigating with these 3 commands are often helpful:
replace 6006 with a meaningful port of your apps URL, in my case I was hunting for localhost:6006 which was not a running app but still available when hitting the URL in browser.
Netstat
netstat -anp tcp | grep 6006
This will report the type of connection and command that is running it, from here you can investigate the TCP states:
tcp4 0 0 127.0.0.1.58473 127.0.0.1.6006 CLOSE_WAIT
tcp4 0 0 127.0.0.1.6006 127.0.0.1.58471 FIN_WAIT_2
tcp4 0 0 127.0.0.1.58471 127.0.0.1.6006 CLOSE_WAIT
tcp4 0 0 127.0.0.1.6006 *.* LISTEN
tcp4 0 0 127.0.0.1.58468 127.0.0.1.6006 TIME_WAIT
lsof
lsof -i tcp:6006
This will list the command name and PID.
Optionally after this, you can use kill along with the PID number to stop the processes from running. ex: kill #####
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 8193 username 26u IPv4 0x8793aae78e568e01 0t0 TCP localhost:6006 (LISTEN)
Google 93430 username 67u IPv4 0x8793aae79530c941 0t0 TCP localhost:58471->localhost:6006 (CLOSE_WAIT)
Google 93430 username 68u IPv4 0x8793aae795d98ba1 0t0 TCP localhost:58473->localhost:6006 (CLOSE_WAIT)
Finally to find where the node process is running you can use ps and here, instead of using node as others have mentioned use the port number itself that you are looking for:
ps
ps -aef | grep 6006
This will report back something like this:
501 8193 8179 0 24May21 ttys008 1:08.98 /Users/username/.nvm/versions/node/v12.10.0/bin/node /Users/username/Documents/repositories/react-application/node_modules/.bin/start-storybook -h localhost -p 6006
501 77757 9725 0 12:50PM ttys013 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox 6006
Now we can confirm the PID from our lsof command matches the PID printed by the ps, we can see when the process was started and we are seeing the path to our persistent app.
Lastly we can use kill #### to stop the process.
I use fkill
INSTALL
npm i fkill-cli -g
EXAMPLES
Search process in command line
fkill
OR: kill ! ALL process
fkill node
OR: kill process using port 8080
fkill :8080
If all those kill process commands don't work for you, my suggestion is to check if you were using any other packages to run your node process.
I had the similar issue, and it was due to I was running my node process using PM2(a NPM package). The kill [processID] command disables the process but keeps the port occupied. Hence I had to go into PM2 and dump all node process to free up the port again.

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