DEV Community

loading...
Cover image for How to kill a node process

Stop Node Server How to kill a node process

Davide de Paolis
Sport addicted, productivity obsessed, avid learner, travel enthusiast, expat, 2 kids. πŸ‚βœˆπŸšžπŸŒπŸ“·πŸ–₯πŸ€˜πŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦πŸš€ (Opinions are my own)
・Updated on ・2 min read

While working on a full-stack application it might happen that you end up with a bunch of node processes running at the same time.
It could be some automation script or a script that watches and executes your unit-tests while you code/TDD.
Most likely it is Webpack running and serving your frontend on localhost and the node app that serves the REST API your frontend is connecting to. In both cases these processes would occupy a specific port.

For example, our current React App is served on localhost:3000 and the backend runs on localhost:3001 via serverless offline.

Normally you would start those processes via the command line with something like:

npm run react-scripts start
Enter fullscreen mode Exit fullscreen mode

or

sls offline start --port 3001
Enter fullscreen mode Exit fullscreen mode

When you are running those, you can quickly shut them down with

 <Ctrl> + C
Enter fullscreen mode Exit fullscreen mode

If you started them via a Debug Configuration in Visual Studio Code or IntelliJ IDEA you can stop the process clicking the Stop button.

stop debugging process

Until here no problem. Sometimes though it happens that you started some process and then despite closing the IDE or the Terminal they still hang there somewhere, and when you try to run them again, then you get errors that the port is occupied.

I don't know why or how that happens, but every now and then ( weeks or months) I find myself googling for the right command to use ( i tend to forget quickly stuff that I don't use often - and that I can google under 20 seconds ). Therefore I will drop it here, it might be useful for someone else too!

ps -ef | grep node
# or 
ps aux | grep node
Enter fullscreen mode Exit fullscreen mode

This commands will print all the node process running, it might be confusing at first since you might have other stuff that is not related to the project you are working on (like Slack or Postman).
Just find the node process pointing to your script or js file and note down the process ID (second value from the left)

node processes running

If you find yourself with a wall of text because you have many processes running, then you could search the processes opened by port ( like normally when I start a react application is on port 3000 while its backend is on port 3001:

lsof -i :3001 
Enter fullscreen mode Exit fullscreen mode

Once you have your process and its ID..

then just kill it without mercy!

kill -9 PROCESS_ID 
Enter fullscreen mode Exit fullscreen mode

Hope it helps

Discussion (6)

Collapse
jesusgollonet profile image
jesΓΊs gollonet

It's usually good to try a kill -15 PROCESS_ID before resorting to kill -9, since it gives the stuck process a chance to clean up before dying.

Probably overkill for local development servers, but definitely preferrable for things that may have state such as a database.

Collapse
dvddpl profile image
Davide de Paolis Author

thanx for pointing this out. :-)

Collapse
mjstelly profile image
Michael Stelly

How did you format your output? Mine appears as a wall o' text like this:

502 1128 1125 0 3:17PM ?? 0:31.12 /Applications/Reactotron.app/Contents/Frameworks/Reactotron Helper (Renderer).app/Contents/MacOS/Reactotron Helper (Renderer) --type=renderer --field-trial-handle=1718379636,9355752010542677649,1429803248351936294,131072 --disable-features=SpareRendererForSitePerProcess --lang=en-US --app-path=/Applications/Reactotron.app/Contents/Resources/app.asar --node-integration --no-sandbox --no-zygote --background-color=#fff --num-raster-threads=2 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --service-request-channel-token=3685712697854128908 --renderer-client-id=5 --no-v8-untrusted-code-mitigations
502 15785 15718 0 6:18PM ?? 0:15.06 /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer) --type=renderer --disable-color-correct-rendering --field-trial-handle=1718379636,9347403567474433047,3772520779013372132,131072 --disable-features=LayoutNG,PictureInPicture,SpareRendererForSitePerProcess --lang=en-US --standard-schemes --secure-schemes=vscode-resource --bypasscsp-schemes --cors-schemes=vscode-resource --fetch-schemes=vscode-resource --service-worker-schemes --app-path=/Applications/Visual Studio Code.app/Contents/Resources/app --node-integration --no-sandbox --no-zygote --background-color=#282a36 --disable-blink-features=Auxclick --num-raster-threads=2 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --service-request-channel-token=1663595825542118109 --renderer-client-id=7 --no-v8-untrusted-code-mitigations
502 18962 15718 0 11:15AM ??

Collapse
sukriti_aggarwal_ac2b9ed4 profile image
Sukriti Aggarwal

Thank you so much. This article really helped me solve a problem that was messing with me.

Collapse
douglasvinicio profile image
douglasvinicio

Thank you! Helped a lot!!!

Collapse
guptshruti2003 profile image
guptshruti2003

Hello I'm unable to kill this DO I have to kill usr/local/bin/node index.js. I tried by its process ID but nothing shows