Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezziemaven/bd65ba3b719c24201b27481aa9d0bab0 to your computer and use it in GitHub Desktop.
Save rezziemaven/bd65ba3b719c24201b27481aa9d0bab0 to your computer and use it in GitHub Desktop.
How to trigger a task on a specific file change in your Node project while nodemon is running

How to trigger a task for a specific file change in your Node project while nodemon is running

I created a script/CLI, env-eg, that helps me easily create an up-to-date .env.example file from a .env file. After using it a couple of times with great success, I wondered if there was a way to automate the execution of the command whenever I saved/ updated my .env file. Moreover, I wondered whether this was possible while my server was still running using nodemon in development mode.

Long story short, after trial and error, even testing whether this was possible through nodemon (it was but was executing the command on every file change instead of only my .env file), I finally figured out the perfect solution. It involves using two npm packages - concurrently and onchange - to make this magic happen:

  • onchange is a simple package that runs a script whenever a file or glob pattern changes.
  • concurrently, as its name implies, runs two or more commands simultaneously rather than in order, and eliminates the need to run separate commands in different terminal windows. Because I needed to run onchange and nodemon - two packages that never exit unless they crash - this package would allow me to run both from the same terminal window whenever I start my server.

Putting them together

Here's my original script that I use to start my server in my package.json file:

"scripts": {
    "start": "NODE_ENV=production node index.js", 
    "dev": "NODE_ENV=development nodemon"
  },

Next, I had to install concurrently and onchange as devDependencies:

$   npm i -D concurrently onchange

where -D is equivalent to --save-dev.

onchange, according to the docs, is used the following way:

$   onchange 'glob-pattern' -- executable

where the glob-pattern is the file or pattern onchange should watch, and executable is the command or script you'd like onchange to run on any watch changes.

concurrently, according to the docs, is used as follows:

$   concurrently "command1 [arg1]" "command2 [arg2]"

which tells concurrently to run command1 with an optional argument arg1 while simultaneously running command2 with an optional argument arg2. When used in an npm script the double quotes need to be escaped, eg.:

"scripts": {
    "script": "concurrently \"command1 [arg1]\" \"command2 [arg2]\""
}

This is my final script which now lets me run onchange and nodemon simultaneously in dev mode:

"scripts": {
    "start": "NODE_ENV=production node index.js", 
    "dev": "concurrently \"onchange '.env' -- env-eg\" \"NODE_ENV=development nodemon\""
  },

Now, any time I make a change to my .env file, I get a log that lets me know that the .env.example file was successfully created while my server is still running.

You can apply this concept to your project if you'd also like to auto-update your .env.example file using my CLI, or to any task you'd like to automate for any file change and keep nodemon running. Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment