Adding nodemon

Adding nodemon

Nodemon is a utility that helps in developing Node.js applications by automatically restarting the application when file changes in the directory are detected. This saves time as you don’t need to manually stop and start the server after every change.

Step 1: Install nodemon

You can install nodemon globally or as a dev dependency in your project.

  • Globally: If you install nodemon globally, you can use it in any Node.js project without needing to install it again.

    bash
    	npm install -g nodemon
  • Locally: If you install nodemon locally as a dev dependency, it will be available only in the project where you installed it.

    bash
    	npm install nodemon --save-dev

    By using --save-dev, you indicate that nodemon is a development dependency and not required in the production environment.

Step 2: Update Your Package.json

To make it easy to run your application with nodemon, you can update the scripts section in your package.json file.

Here’s how you can do it:

json
	{
	  "name": "your-project-name",
	  "version": "1.0.0",
	  "main": "index.js",
	  "scripts": {
	    "start": "node index.js",
	    "dev": "nodemon index.js"
	  },
	  "devDependencies": {
	    "nodemon": "^2.0.0"
	  }
	}
  • The start script runs your application using Node.js, as usual.
  • The dev script runs your application using nodemon. When you run this script, nodemon will watch your files and automatically restart the server whenever it detects a change.

Step 3: Run Your Application with nodemon

To run your application with nodemon, use the following command:

bash
	npm run dev

When you make changes to your files, nodemon will automatically restart the server, allowing you to see your changes immediately without manually restarting the server.

nodemon running

Example

Let’s assume you have the following simple server setup in index.js:

javascript
	const http = require('http');
	
	const server = http.createServer((req, res) => {
	  res.writeHead(200, { 'Content-Type': 'text/plain' });
	  res.end('Hello, World!\n');
	});
	
	server.listen(3000, () => {
	  console.log('Server running at http://localhost:3000/');
	});

After adding nodemon, you can modify the res.end message and simply save the file. nodemon will detect the change and restart the server for you:

javascript
	res.end('Hello, nodemon!\n');

Once you save this file, nodemon will restart the server, and when you refresh http://localhost:3000/, you’ll see the updated message.

Conclusion

Nodemon is a powerful tool that enhances your development workflow by automating the server restart process. By integrating nodemon into your project, you can focus more on coding and less on managing server restarts, significantly improving productivity.

Lesson task

We will use nodemon to automatically restart the server when changes are made to the code.

Goal

Demonstrate that you can use nodemon to automatically restart the server when changes are made to the code.

Brief

Add nodemon globally or to your project, then create an alias called dev that runs the server using nodemon.

NOTE: Lesson tasks do not get submitted on Moodle and are not assessed by tutors. They are mainly there for you to practise what you have learned in the lesson.