Are you trying to Restart Node.js Apps Automatically with Nodemon?
This guide is for you.
Nodemon is a tool that helps develop node. js based applications by automatically restarting the node application when file changes in the directory are detected. nodemon does not require any additional changes to your code or method of development.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Node.js related tasks.
In this context, we shall look into how to Restart Node.js Apps automatically.
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 to restart node.js apps automatically with Nodemon are given below:
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.
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.
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.
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
This article will guide you on how to restart node.js apps automatically with #nodemon. If it's just running (not a #daemon) then just use Ctrl-C . Where PID is replaced by the number in the output of ps . nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
To start Nodemon with #NPM:
1. #Install nodemon. Since nodemon is a command line tool, it has to be installed as a global node package.
2. #Boot up the Node server. First, make sure that #MongoDB is already running in the background.
3. Add nodemon to package.json as an #NPM script.
4. Start the Node server via NPM.