16 Jun

nodemon leaving Node.js process running

Category:General PostTag: :

nodemon is a great tool for use when writing Node.js applications. It is a simple little tool that simply watches for changes in the file system of your project and restarts the Node.js application when a file changes.

Of course, there are more options, but that’s the essential idea. Unfortunately, once you launch nodemon it is off and running and the only way to stop it is to brek out of the command shell with CTRL+C.

Doing this can leave a running instance of your Node.js applciation running as a background process. HUGELY annoying, especially when your app is a long-running application, like a web application.

Fortunately, nodemon subscribes to a couple of Node events and we can tap into those if we wrap nodemon in a JavaScript task runner like Gulp.

Below is my simple gulpfile.js which starts nodemon and has it watch for certain events that will be emitted when shutting down the nodemon process. This catches the exiting nodemon process and kills the Node.js process it spawned.

[gist id=024a68e97bd8f541e2c30fc6d898fd79 bump=1]