Using Nodemon for Speedy Development with Node.js
Just began working with Node.js development and encountered some repetitive tasks, like having to restart the server every time a small change is made locally. These tasks can cause delays and lead to procrastination, ultimately reducing productivity. Let’s explore how Nodemon can assist in these situations.
Using third party libraries
Not everything needs to be built from the ground up. To streamline repetitive tasks, developers worldwide have created libraries that others can benefit from. Consider the scenario of repeatedly restarting the server to apply changes. While it’s possible to handle this without a library, why reinvent the wheel when a ready-made solution exists? Simply install and use it.
What we will be building
We will be using the current Foodie backend project and installing nodemon to remove the repetitive process of restarting the server again and again after any change. Let’s clone the project
Clone the project
Open a terminal, navigate to the location where you want to set up the project, and run the following command:
git clone https://github.com/devnur-org/foodie-be
Navigate to the project directory
cd foodie-be
Installing dependencies
Next, install the project’s dependencies by running the following command:
npm install
Create a new branch (optional)
git checkout -b nodemon
Excellent! The project is now set up and ready for you to proceed with this tutorial. To achieve the current project stage, follow the steps outlined below:
Installing Nodemon
Let’s proceed by installing the nodemon library in our project. Open a terminal in your project directory and execute the following command.
npm install -D nodemon
To integrate nodemon into our project, we need to modify the start script in the package.json
file. By default, the npm start
command runs node server.js
, but we’ll update it to the following.
{
"name": "foodie-be",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"nodemon": "^3.1.4"
}
}
Run the server using the following command
npm start
To check if changes are applied automatically, modify the text sent in the response of the server.js file within the get request, then save it.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('<h1>Hello from Nodemon!</h1>');
})
app.listen(3000, () => console.log('Server running on port 3000'));
The moment you save the file, the changes are applied, and the server restarts automatically, saving us from manually restarting it. This means the tool is working as expected. Congratulations 🎉 on adding your first tool to your Node.js and Express project! Nodemon is widely used by millions of projects and offers further customization. Below is the link to the Nodemon repository on GitHub.
https://github.com/remy/nodemon