[javascript] stop all instances of node.js server

This is my first time working with Node.js and I ran into this problem:

I have started a Node server through the plugin of an IDE. Unfortunately, I cannot use the IDE's terminal. So I tried to run the script from the command line.

This is the problem - I am using the Express module and my app is listening some port (8080). When I start the app from the command line, it throws this error:

events.js:71
    throw arguments[1]; // Unhandled 'error' event
                   ^
Error: listen EADDRINUSE
    at errnoException (net.js:770:11)
    at HTTPServer.Server._listen2 (net.js:910:14)
    at listen (net.js:937:10)
    at HTTPServer.Server.listen (net.js:986:5)
    at Object.<anonymous> (C:\xampp\htdocs\node\chat\app.js:5:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

Even though I am not very sure what this error could be I assumed that it's because the app is listening on a port which is already in use. So I did:

netstat -an

I can see

TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING

It's because the Node server is already started when I tried to start it from the IDE.

So I want to know, how can I stop all server instances? Also if you can tell me how to detect what's running on a port and kill it.

This question is related to javascript windows node.js express port

The answer is


Multiplatform, stable, best solution:

use fkill to kill process which is taking your port:

fkill -f :8080

To install fkill use command: npm i -g fkill


it works fine in windows 10

taskkill /f /im node.exe

you can try

killall node

you can also try

killall nodejs

Press in cmd or bash : Ctrl + C


If you are using Windows, follow this:

  1. Open task manager, look for this process: Task manager showing Node process - Node.js Server-side JavaScript

  2. Then just right click and "End task" it.

  3. That's it, now all the npm commands run form the start.


The fastest way is

killall node

Works with Linux, OS X


You could also try:

killall nodejs


You can use lsof get the process that has bound to the required port.

Unfortunately the flags seem to be different depending on system, but on Mac OS X you can run

lsof -Pi | grep LISTEN

For example, on my machine I get something like:

mongod     8662 jacob    6u  IPv4 0x17ceae4e0970fbe9      0t0  TCP localhost:27017 (LISTEN)
mongod     8662 jacob    7u  IPv4 0x17ceae4e0f9c24b1      0t0  TCP localhost:28017 (LISTEN)
memcached  8680 jacob   17u  IPv4 0x17ceae4e0971f7d1      0t0  TCP *:11211 (LISTEN)
memcached  8680 jacob   18u  IPv6 0x17ceae4e0bdf6479      0t0  TCP *:11211 (LISTEN)
mysqld     9394 jacob   10u  IPv4 0x17ceae4e080c4001      0t0  TCP *:3306 (LISTEN)
redis-ser 75429 jacob    4u  IPv4 0x17ceae4e1ba8ea59      0t0  TCP localhost:6379 (LISTEN)

The second number is the PID and the port they're listening to is on the right before "(LISTEN)". Find the rogue PID and kill -9 $PID to terminate with extreme prejudice.


Use the following command to kill and restart node server from batch file

    @echo off
cd "D:\sam\Projects\Node"
taskkill /IM node.exe -F
start /min cmd /C "node index.js"
goto :EOF

You can try this:

taskkill /IM node.exe -F

in windows command Type command blow:

taskkill /f /im node.exe


Linux

To impress your friends

ps aux | grep -i node | awk '{print $2}' | xargs  kill -9

But this is the one you will remember

killall node

Windows & GitBash Terminal I needed to use this command inside Windows / Webstorm / GitBash terminal

cmd "/C TASKKILL /IM node.exe /F"

Am Using windows Operating system.

I killed all the node process and restarted the app it worked.

try

taskkill /im node.exe

if you want to kill a specific node process , you can go to command line route and type:

ps aux | grep node

to get a list of all node process ids. now you can get your process id(pid), then do:

kill -9 PID

and if you want to kill all node processes then do:

killall -9 node

-9 switch is like end task on windows. it will force the process to end. you can do:

kill -l

to see all switches of kill command and their comments.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to express

UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block jwt check if token expired Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] npm notice created a lockfile as package-lock.json. You should commit this file Make Axios send cookies in its requests automatically What does body-parser do with express? SyntaxError: Unexpected token function - Async Await Nodejs Route.get() requires callback functions but got a "object Undefined" How to redirect to another page in node.js

Examples related to port

Docker - Bind for 0.0.0.0:4000 failed: port is already allocated How do I kill the process currently using a port on localhost in Windows? Node.js Port 3000 already in use but it actually isn't? Can't connect to Postgresql on port 5432 Spring Boot - How to get the running port Make docker use IPv4 for port binding How to change the default port of mysql from 3306 to 3360 Open firewall port on CentOS 7 Unable to launch the IIS Express Web server, Failed to register URL, Access is denied XAMPP Port 80 in use by "Unable to open process" with PID 4