We all love Node for its simplicity. We can create a basic index.js file and start writing an application. We just type node index.js inside our terminal and our script will execute when we want to run it.
However, we will soon notice that any additional changes made to our code, in case that we are running a http server or some other long running process, will not restart our script and our code will not have an immediate effect.
We would have to manually terminate our script and run node index.js again.
During development, this would probably drive us insane. Not even mentioning how unproductive and inefficient this would be.
In order to mitigate this problem, we can utilize one small, but highly effective tool called nodemon. When we run our script with nodemon, it will actively lookout for any code changes and restart our script if any were detected.
We can install nodemon from NPM.
npm i –g nodemon
Now instead of
node index.js
we can do
nodemon index.js
This setup will work perfectly fine but we can one up this even further.
We can add an additional script in package.json called start or any other name that you prefer
{
...
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
...
}
Now instead of
nodemon index.js
we can just run
npm start
Sweet, isn’t it.