Automatically Reload Node.js App After Saving
May 30, 2020 by Andreas Wik
Sick of running node myapp.js every time you’ve made a change to your script? Use nodemon to reload your app every time you save changes to your app’s files.
First, if you haven’t already, make sure to initialize your app with npm (npm comes with your Node.js installation). To do this, navigate to your project’s folder and run npm init. You will be prompted with a few questions. You can either answer them or just hit Enter for each to leave them with default answers. Another option is to run npm init -y and it won’t ask you any questions.
Next, install nodemon by running npm install nodemon.
Once you got this installed, you can now run nodemon myapp.js instead of node myapp.js. When you make a change and save your file, the app will now re-run automatically.
Watch different types of files
You may want to make sure that nodemon restarts your app when you make changes to any file in your project, for example both .css and .js files, and maybe .hbs if you’re using Handlebars.
Just add on the -e flag and then list the extensions you want nodemon to watch, separated by commas.