Wondering how to Restart Node.js Apps Automatically with Nodemon?
At Bobcares we often handle similar requests from our customers as a part of our Server Management Services.
Today let’s see how our Support Engineers do this for our customers with ease.
How to Restart Node.js Apps Automatically with Nodemon?
We often need to restart the process after making changes so that they come into effect.
To avoid this step we can use Nodemon to restart the process automatically.
Some other features Nodemon includes default support for node & coffeescript, detecting default file extension to monitor. and working with server applications or one time run utilities and REPLs.
The steps our Support Engineers use to restart node.js apps automatically with Nodemon are given below:
1. Installing Nodemon
We can begin by installing nodemon.
Installation of the utility can either be done globally or locally using npm or Yarn:
Global Installation
npm install nodemon -g
Or
yarn global add nodemon
Local Installation
npm install nodemon --save-dev
Or with Yarn:
yarn add nodemon --dev
It should be kept in mind that with a local installation that cannot use nodemon command directly from the command line.
We can use it as part of any npm scripts or with npx.
[Need assistance to install? We are here for you!]
2. Example of Express Server with Nodemon
We can use Nodemon to start a Node script.
If we have an Express server setup in a server.js file, we can use the following command to start it and check the changes:
$ nodemon server.js
The following command can be used if we are running the script with Node:
$ nodemon server.js 3006
Each time we make a change to a file with one of the default watched extensions (.js, .mjs, .json, .coffee, or .litcoffee) in the current directory or a subdirectory, the process will restart.
We will consider the example of server.js file that outputs the message: Alpha app listening on port ${port}!.
$ nodemon server.js
We see the following terminal output:
Output [nodemon] 1.17.3 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node server.js` Alpha app listening on port 3000!
While nodemon is still running, we will make a change to the server.js file to output the message: Beta app listening on port ${port}!.
We can see the following additional terminal output:
Output [nodemon] restarting due to changes... [nodemon] starting `node server.js` Beta app listening on port 3000!
We can restart the process at any time by typing rs and hitting ENTER.
Alternatively, nodemon will also look for the main file specified in the project’s package.json file:
package.json { // ... "main": "server.js", // ... } Or, a start script: package.json { // ... "scripts": { "start": "node server.js" }, // ... }
After making the changes to package.json, we can call nodemon to start the example app in watch mode without passing in server.js.
3. Options available in nodemon
We can modify the configuration settings available in nodemon.
Some of the options available are:
1. --exec: To specify a binary to execute the file with. 2. --ext: To specify different file extensions to watch. 3. --delay: By default, nodemon waits for one second to restart the process when changes are made in files, but with the --delay we can specify a different delay. 4. --watch: To specify multiple directories or files to watch. 5. --ignore: To ignore certain files, file patterns, or directories. 6. --verbose: A more verbose output with information about what file(s) changed to trigger a restart.
The available options can be seen with the following command:
$ nodemon --help
Using these options, we will create the command to satisfy the following scenario:
1. Watching the server directory 2. Specifying files with a .ts extension 3. Ignoring files with a .test.ts suffix 4. Executing the file (server/server.ts) with ts-node 5. Waiting for three seconds to restart after a file changes
$ nodemon --watch server --ext ts --exec ts-node --ignore '*.test.ts' --delay 3 server/server.ts
This command combines –watch, –ext, –exec, –ignore, and –delay options to satisfy the conditions.
4. Using Configurations
Adding configuration switches when running nodemon becomes difficult sometimes.
So we can mention the changes in the configurations nodemon.json file.
The following shows changes made to configuration nodemon.json
nodemon.json { "watch": ["server"], "ext": "ts", "ignore": ["*.test.ts"], "delay": "3", "execMap": { "ts": "ts-node" } }
We will use of execMap instead of the –exec switch.
execMap allows specifying binaries that should be given to certain file extensions.
Finally, we can start nodemon with the script:
nodemon server/server.ts
nodemon picks up the configurations and uses them
[Need assistance? We are here for you!]
Conclusion
To conclude we saw how to restart node.js apps automatically with nodemon. Also, we saw the method used by our Support Engineers to implement this.
0 Comments