22
Restart a Node.js app automatically
When developing Node.js applications you have to restart the application each time changes are made to a file, otherwise your changes will not be applied and not be visible in the application. You can manually do this, by stopping the running node process and running it again. Though, this task can be automated with the help of nodemon. The tool nodemon helps develop node.js based applications by automatically restarting the node application, when file changes in the directory are detected.
A typical Node.js applications has many npm scripts, of a few are start
(to start the application) and dev
(to start the development version). When using nodemon, it is recommended to create a npm script specifically for running the application during development, can be called dev
or development
or whatever you like, though dev
is mostly used. Creating scripts for specific environments is best practice for separating commands and avoid bugs.
Nodemon can be installed globally, or it can be installed locally as a devDependency.
First steps it to create a start script, which will run the application. Add an entry in package.json under the "scripts" field. It should look like this (assuming index.js would start the application):
"scripts": {
"start": "node index.js"
}
If you have installed nodemon globally, you can skip creating a dev script and just run the nodemon from the command line. Though everyone working on the project should have all the tools to get started. Globally-installed packages aren't distributed with your project, this means that if someone does not have nodemon installed globally they would not be able to use it within this project.
To install nodemon as a devDependency run:
npm i -D nodemon
# OR
npm install --save-dev nodemon
After successfully installing nodemon, we can create a dev script, which will reload our app on file changes during development. The package nodemon has an --exec
flag to run an npm script whenever a file has changed.
To create a dev script, add a new "dev"
entry in the scripts field in package.json
.
"scripts": {
"start": "node index.js",
"dev": "nodemon --exec 'npm start'"
}
In the dev script above, we tell nodemon to run the npm start script when a file has changed. One of the benefits of npm scripts is that they are able to run devDependencies without having to use the full path to the executable in the node_modules directory. By default, nodemon will try to restart index.js
.
Finally, run the dev script by typing npm run dev
and enjoy nodemon restarting your application on file changes. To manually restart your Node.js application you can type rs
in the command line.
Important: If your application writes to files, like updating a config file, nodemon will prematurely restart your application. To prevent this behaviour, nodemon has a flag for ignoring files or folders, and you can also use wildcards, for example: --ignore './folder/*.json'
.
- Creating npm scripts with unique purposes is useful to organize the tasks.
- The package nodemon can automatically restart Node.js application in development mode.
- Nodemon should be installed as a devDependency to provide the same tools to every person working on the project.
- Npm scripts can be executed by nodemon with the
--exec
flag. - Files and folders can be ignored with the
--ignore
flag in nodemon, which prevents premature restarts of the application.
Thanks for reading and if you have any questions , use the comment function or send me a message @mariokandut.
If you want to know more about Node, have a look at these Node Tutorials.
22