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
sls offline start --port 3001
When you are running those, you can quickly shut them down with
<Ctrl> + C
If you started them via a Debug Configuration in Visual Studio Code or IntelliJ IDEA you can stop the process clicking the Stop button.
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
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)
then just kill it without mercy!
kill -9 PROCESS_ID
Hope it helps