VS Code Remote for Node.js caveat: dealing with detached nodemon process

Table of Contents

I develop all my new projects on a remote Hetzner Cloud machine, using wonderful and almost too-good-to-be-true VS Code Remote.

I recommend this setup for everyone who does not like spinning fans of their Macbooks - and I really prefer Ubuntu and real Linux environment to brew when I setup my web servers, databases, node.js and other infrastructure.

Using VS Code Remote terminal for Node.js development has one caveat though: if I use nodemon which reloads the script on every file save, from time to time my ssh connection is lost and VS code fails to restore this connection. When I reconnect to ssh terminal, and try to launch new nodemon process, I get This port is already being used by another process exception.

Related GitHub issue:


Here are two advices how to mitigate the issue:

1. Use tmux

Using tmux for long running processes is a good solution in most of cases. When you re-attach your VS Code terminal to server, you just do tmux ls  to view your active sessions and and then tmux a -t sessionname

Tmux is perfect when you learned all its hotkeys: https://gist.github.com/MohamedAlaa/2961058

The only bad thing about tmux which irritates me is that there is no easy way (that I am aware of) to make simple copy&paste work like it works when regular VS code remote terminal is used.

But, sometimes I forget to launch all my nodemon processes inside tmux... so here is the second lifehack:

2. Quickly locating and killing detached nodemon process

When my VS code remote disconnects and looses my nodemon terminal after window reload, I always spend 3-4 minutes googling how to quickly kill the nodemonized process. So this writeup is created mostly for myself because I failed miserably memorizing this, since I use it only from time to time. Now I can just open this page and copy&paste the commands!

  1. Get pid by port (2355 in my case):

sudo lsof -n -i :2355

if you kill the found PID it won't help, since nodemon will restart it. So, get copy PID from output (63073 in my case) and..

2. Now find the parent of this process (nodemon):

ps -o ppid= -p 63073

3. Make sure this is the correct PID of nodemon (50461 was the output of the previous command):

ps aux | grep 50461

4. Kill nodemon process by PID kill 50461

Voila! Now the new nodemon process can be launched.