Prevent Nodemon from Constantly Restarting

Sep 18, 2020 min read

It’s not often that I run across this issue, but it does happen from time to time where Nodemon seems to constantly restart. If you’re not familiar, Nodemon is a fantastic NPM package that will watch for file changes and restart your Node process automatically. This really speeds up development as you don’t need to stop and restart the process to test every change.

The issue comes when some files in your project folder update frequently. I recently ran into this when I was using the LowDB package to have a local JSON based database. The JSON file that’s created is refreshed frequently and because that file is saved along side the project, Nodemon will pickup on the file change and restart the Node process, leading to vicious circle of restarting and frustration.

The Nodemon API allows us to ignore files to be watched, which is exactly what we need. There a couple of ways to tell Nodemon to cool it with the automated restarts.

  1. Pass the files to ignore in the terminal command
nodemon --ignore '[files to ignore]' app.js
  1. Configure the files to ignore in the package.json file (preferred since it persists). We can pass an array of directories or files to ignore in the nodemonConfig object.
...
"nodemonConfig": {
  "ignore": [
    "db.json"
  ]
},
...
  1. Create a nodemon.json file with all of the values we want to pass to Nodemon
...
"ignore": ["tests/"]
...