While developing your node.js application, you may get tired of restarting my server after each change made on the back end. Nodemon is a CLI (command-line interface ) utility that can wrap your node.js application. It can watch the files in your server folder and automatically restart when changes are detected.
What is the great thing about getting started with nodemon? It does not require any additional changes to your code or your method of development.
You can install it locally for just your current application or even install it globally for all of your projects.
Global install
$ npm install nodemon -g
What is the benefit of installing locally? It will get listed as one of the developer dependencies on your project in the package.json file. This way, you can create a script to run Nodemon that you and the rest of your team can use.
Local Install
npm install nodemon --save-dev
Getting started
Nodemon can handle the same arguments passed to a Node script. If your node.js or express setup is in a server.js or an index.js file, begin your application like you would with Node.
$ nodemon server.js
Like running your script with Node, you can pass in arguments at the end.
$ nodemon server.js 8080
By default nodemon will watch the following extensions in the specified directory or any of it's sub-directories.
- .js
- .mjs
- .json
- .coffee
- .litcoffee
If you need to restart your application while Nodemon is running manually. Instead of stopping and restarting your app, you can type rs
and then enter. This action will do a quick reboot of your process.
If at any point you would like to look at the available CLI options, then you can use -h.
nodemon -h
Here are a few of the main options that you have access to:
--delay
Maybe you would like to wait for several files to change before restarting your application in some situations. By default, the timeout for checking for changes is one second. You may not want to restart your application multiple times unnecessarily; you can use the delay option as a throttle.
The below command will have Nodemon wait for 8 seconds to restart the server after detecting a change.
nodemon --delay 8 server/index.js
You can also specify the wait time in milliseconds using a float or the time specifier (ms). The following two commands will have Nodemon wait for 3.5 seconds.
nodemon --delay 3.5 server/index.js
nodemon --delay 3500 server/index.js
exec
Suppose you would like to run non-node scripts. You can specify other programs to execute and run. For example, if you are running typescript files, then you are likely using ts-node.
nodemon --exec "ts-node" server/index.ts
ext
With ext, you can specify what type of extensions you would like Nodemon to watch. Provide a comma-separated list of the file-extensions.
nodemon server/index.js --ext js, ts
watch
Every file in the directory that matches the default or specified file type will be run by default. Would you like to be more direct about the files you watch for changes? Then you can specify them with the --watch
option.
In the below command example, Nodemon will only watch for changes in the app or libs directory.
nodemon --watch app --watch libs server/index.js
ignore
If you would also like to ignore specific directories, you can use the following --ignore
command.
nodemon --ignore lib/ --ignore test/ server/index.js
You can also specify a file to ignore
nodemon --ignore lib/app.js
Of course, the best way to use Nodemon is by creating a script for it inside your package.json.
"scripts": {
"start": "...",
"dev-server": "nodemon server/index.js",
}
You can also add configurations in your package.json with any of the available options.
"nodemonConfig": {
"watch": [
"server"
],
"ext": "ts",
"ignore": [
"*.test.ts"
],
"delay": "3.5",
"execMap": {
"ts": "ts-node"
}
},
That concludes this walkthrough. Nodemon is one of those npm packages that you wish you would have learned about sooner. But one of the most exciting things about software development is the surprising utilities you find when not even looking. Thanks for reading, and I hope you have learned about a useful tool that can help you speed up your application development.
Stay Focused || Love your code!
Resources:
- Nodemon npm
- [Restart You Apps Using Nodemon] https://www.digitalocean.com/community/tutorials/workflow-nodemon)
- Using express and nodemon
Top comments (0)