nodemon leaving Node.js process running

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.

Published by

David Starr

David Starr is a professional software craftsman committed to improving agility, collaboration, and technical excellence in software development teams. David is the Technical Training Director for GoDaddy. He is the founder of Elegant Code Solutions, has served in numerous leadership contexts, and was as an early and consistent advocate for agile software development.