Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Nodemon is a tool that aids in the development of node.js apps by automatically restarting applications when file changes in the directory are detected.
When you make modifications to a node app, you must restart the server to see the changes take effect. However, when you use Nodemon to launch your node.js application, it will automatically monitor changes and restart the server, if required.
Installation
Install nodemon using npm (recommended) -
npm install -g nodemon # -g flag globally installs nodemon to the system path.
Install nodemon using yarn -
yarn global add nodemon
For installing nodemon as a development dependency -
npm install --save-dev nodemon # or using yarn: yarn add nodemon -D.
However, installing nodemon as a dev-dependency will not enable you to use nodemon directly in the command line. Instead, you may call nodemon from within an npm script to run the local installation.
Usage
Nodemon encapsulates your application, allowing you to pass it all of the arguments you'd typically pass to it.
nodemon [your node app]
To explore the available options -
nodemon -h # or nodemon --help.
To run your application using nodemon, use the following command -
nodemon [your application] [your arguments]
Example -
nodemon ./server.js localhost 8080
This script's output is prefixed with [nodemon]; otherwise, all output from your program, including errors, will be echoed out as usual.
Create a helloworld.js file and include the following code in it -
console.log("Hello world!");
Run the JS file using nodemon -
nodemon helloworld.js
If you make any changes in the file helloworld.js and save it, you will find that the nodemon script restarts automatically, accounting for the change.
Changed helloworld.js -
console.log("Changed Hello world!");
Manual Restart
Suppose you need to restart your program while nodemon is running manually. Instead of stopping and restarting nodemon, type rs followed by a carriage return, and nodemon will restart your process.
Ignoring files
Nodemon will only restart if it detects any JS file changes. To prevent nodemon from resuming your program prematurely, you may choose to ignore some specified files, folders, or file patterns.
Use the following syntax to do the same -
nodemon --ignore lib/ --ignore tests/ # ignore all files in lib and tests directory.
For specific files -
nodemon --ignore lib/app.js
For patterns -
nodemon --ignore 'lib/*.js'
By default, nodemon will ignore the bower_components, .git, node_modules, coverage and .sass-cache directories. You must modify the underlying default ignore rules if you wish to watch a directory like node modules.
Restart after a pause
In some cases, you might wish to wait until many files have been updated before proceeding. Before checking for new file modifications, there is a one-second timeout. If you're uploading a large number of files in a short amount of time, your app may restart several times unnecessarily.
Use the --delay command to add an extra throttle or delay restarting:
If specified, the delay figure is the number of seconds (or milliseconds) to delay before restarting. As a result, nodemon will only restart your program once the specified amount of seconds have passed since the last file update.
If you set this value in nodemon.json, it will always be translated into milliseconds. For example, the following are equivalent:
nodemon --delay 2.5
{ "delay": 2500 }
Execute functions when nodemon state changes
Nodemon states/events
start - child process has started
crash - child process has crashed (nodemon will not emit exit)
exit - child process has cleanly exited (i.e., no crash)
restart([ array of files triggering the restart ]) - child process has restarted
config: update - nodemon's config has changed
Usage of nodemon events/states
Create a file named triggerfunction.js and copy the following code into it -
var nodemon = require('nodemon'); // include nodemon as a module.
Create a file hello.js and write the following code into it -
console.log("Hello world!!");
Run the triggerfunction.js file -
node triggerfunction.js
Now make changes to the hello.js file and see the effect.
Now quit the program and see the output -
Nodemon Event Listeners
Nodemon event listeners working is similar to that of nodemon events. To have a look, replace the content of the triggerfunction.js file with the following and repeat the same steps.
var nodemon = require('nodemon'); // include nodemon as a module.
Nodemon monitors the current working directory by default. Use the
--watch option to add specific pathways if you wish to take control of that option:
nodemon --watch app --watch libs app/server.js
Nodemon will now only restart if the./app or./libs directories have changed. There's no need to include sub-directories because nodemon will traverse them by default specifically.
Tracking multiple extensions
Nodemon checks for files with the extensions.js,.mjs,.coffee,.litcoffee, and.json by default. Nodemon will monitor files with the extension.py if you use the —exec option and monitor app.py. You can, however, use the -e (or --ext) flag to define your own list, as follows:
nodemon -e js,pug,py
Frequently asked Questions
Q1. What is nodemon used for?
Ans: Nodemon is a tool that will keep an eye on your source code and restart your server if anything changes. It is highly recommended for developers to develop Node.js server-based applications using nodemon.
Q2. Does Nodemon work with Python?
Ans: Nodemon can track changes in any file. By default, it tracks changes in .js, .mjs, .json files. However, using the syntax specified in Tracking multiple extensions, it can track changes in any file.
Q3. Where is nodemon installed?
Ans: Nodemon will be installed on your system's global path. Nodemon will not be available in your system path if you install it locally, and you will not be able to use it directly from the command line.
Q4. Does Nodemon work with TypeScript?
Ans: You can also use Nodemon with TypeScript instead of JavaScript.
Key Takeaways
In this article, we learned the significance of nodemon in development. Nodemon is a great tool when it comes to tracking any file changes and restarting the application. It was originally developed to handle server-based applications. We learned how to install nodemon both globally and locally as dev-dependency.
We also learned how to ignore specific files in the working directory. This is specifically required if we don’t want to restart the whole application when the application is not affected by a few files.
We also learned about nodemon states and events and how to fire event listener functions. We also learned how to monitor directories other than the working directory and support file change tracking in other extensions.