In this Node.js tutorial, I will tell you how to install and configure nodemon to auto-restart the basic Node.js web application with express.js server. We’ll create a basic node app and if any change occurs in the app then nodemon will automatically restart the server for us.
The nodemon is an npm module developed by @remy. The nodemon is used with Node.js applications and helps in automatically restarting the node.js application when any change is made in the project files. Nodemon is super easy to use and doesn’t require any tedious configuration. This module is downloaded around 1, 547, 346 times weekly, it comes under MIT license and can be used by anybody.
To understand this topic clearly, we first need to create a basic Node.js project. You must have a Node js set up in your system to get started with Node. If you are new to Node js.
Use the below command to create basic node app directory:
mkdir basic-node-app
Enter into the project directory:
Run command to initialize node js project. It creates the package.json
file. In this file, all the node packages and project settings are registered.
npm init
It asks for the following questions:
package name: (basic-node-app)
version: (1.0.0)
description: A basic node js app with express js
entry point: (index.js)
test command:
git repository:
keywords:
author: Digamber
license: (ISC)
Answer the questions, it will generate the package.json
file in your project directory.
{
"name": "basic-node-app",
"version": "1.0.0",
"description": "A basic node js app with express js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Digamber",
"license": "ISC"
}
The package.json
holds the module configuration used in the specific project.
In this step, we will install express js to configure express.js server in our node application.
Run command to install express:
npm install express
Create a server.js file in the root directory of your project and add the following configuration:
In server.js, we imported the express library and bound it to the app variable. We declared the port 8081, and the app will run on this port.
In this nodemon tutorial we’ll look at how to get rid from restarting the application again and again. For automating the starting process in Node app, we can either install nodemon locally or globally.
Let’s install the nodemon globally first.
// Install nodemon globally with Node JS
npm install nodemon -g
// Install nodemon globally with Yarn
yarn global add nodemon
Since we are in the development phase, so it will be a better practice. If we install the nodemon package with --save-dev
Tag. This parameter will register nodemon package in devDependencies array in pacakge.json
.
// local Install nodemon with Yarn
yarn add nodemon --dev
// local Install nodemon with Node JS
npm install nodemon --save-dev
To watch changes in Node app we need to tweak a little bit in package.json
.
{
"main": "server.js",
}
{
"scripts": {
"start": "node server.js"
},
}
It’s time to check how does nodemon is working in our app, open the terminal and start the app using the following command:
nodemon server.js
# [nodemon] 1.19.1
# [nodemon] to restart at any time, enter `rs`
# [nodemon] watching: *.*
# [nodemon] starting `node server.js`
# App is working!
If you see above message, it means your app is working fine. Now important thing is, if you make any changes in the file. You’ll see nodemon is restarting the server in the backend automatically.
The Final package.json
:
{
"name": "basic-node-app",
"version": "1.0.0",
"description": "A basic node js app with express js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "Digamber",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.19.1"
},
"dependencies": {
"express": "^4.17.1"
}
}
Nodemon offers advance configuration options to manage automatic server processing. Let’s check them out one by one.
Nodemon can also execute and watch other programs. It can read the other files instead of the .js
extension.
nodemon --exec "python -v" ./myapp.py
In the above example, nodemon will run the myapp.py
file and execute the .py
extension of a python program.
Define Non Executables in nodemon.json
Nodemon also gives you the freedom to define the programming language, which is not supported by nondemon. Go to the nodemon.json
config file and declare the language support.
{
"execMap": {
"pl": "perl"
}
}
Run the command in terminal to execute non-supported nondemon language:
nodemon script.pl
By default, nodemon watch an existing working directory. However, In the below example, we will see how does nodemon keep an eye on multiple directories.
nodemon --watch app --watch server app/server.js
Now, as per the example, nodemon will restart if any change occurs in the ./app
or server
directory. We don’t need to define the su-directories because nodemon also checks the sub-directories while executing the parent directory.
You can declare extensions of your choice in nodemon. However if you don’t define any extension then nodemon will by default look for .json, .mjs, .coffee, .js and .litcoffee extension.
nodemon -e jade, js
The nodemon restarts the server as soon as you make any change in the app. However, you can delay the restarting process, nodemon offers –delay tag to delay the restarting process.
nodemon --delay 15 server.js
you can even delay the restarting by milliseconds:
nodemon --delay 5.5 server.js
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Show Image Preview Before Upload in VueJs
If you optate to show image preview afor...How To Create a Web Server in Node.js with the HTTP Module
Node.js has built-in HTTP module to tran...How to show and hide div elements based on dropdown selection in jQuery
Use the jQuery change() method The fo...Laravel Validation In Bootstrap Model Using Ajax
Today, Laravelcode share with you how to...Laravel 8 Paypal Payment Gateway Integration
Paypal is a international payment proces...