nodemon And package.json scripts

Introduction:

In the previous topic we explain that npm has the ability to install modules(packages) globally,by adding the –global flag when installing a module. A good example of a module that is installed globally is nodemon.

When developing a Node.js project, you need to restart the application each time changes are made to a file in order to see the changes reflected in the application. You can do this manually by stopping the running process and running it again from the command line, but that gets old real quick.

Instead, we can use a command line tool called nodemon to help us restart the project whenever a js file within the project has changed. Nodemon can be installed globally, or it can be installed locally into your project as a devDependency.

Learning Objectives:

  • What is nodemon module?
  • Why to use nodemon?
  • install nodemon.
  • what an npm script is in Node
  • how to run scripts with npm
  • Explain the difference between custom and default scripts

Study

What is nodemon?

  • 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.
  • nodemon is a replacement wrapper for node.
  • To use nodemon, replace the word node on the command line when executing your script.
  • Nodemon will look at the file you pass and it will run the node command every time that file changes.

Installation

You have two ways ,

  • Either through cloning with git
  • or by using npm (the recommended way).

in your terminal run the following command

npm install -g nodemon

or using yarn:

yarn global add nodemon

And nodemon will be installed globally to your system path.

You can also install nodemon as a development dependency(locally): npm install --save-dev nodemon

One thing to be aware of with a local install is that you will not be able to use the nodemon command directly:

$
Output
command not found: nodemon

You can execute the locally installed package:

./node_modules/nodemon/bin/nodemon.js [your node app]

You can also use it in npm scripts or with npx.

npm Scripts

An npm script is a convenient way to bundle common shell commands for your project. They are typically commands, or a string of commands, which would normally be entered at the command line in order to do something with your application.

Scripts are stored in a project’s package.json file, which means they’re shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.

  • Common use cases for npm scripts include building your Node project,
    • starting a development server,

    • compiling CSS,

    • linting,

    • minifying,

    • or anything else you find yourself typing into your terminal frequently that’s related to your project.

  • Scripts are executed using the npm run {SCRIPT_NAME} command.
  • You can run the npm run command without any additional arguments to see the complete list of scripts defined for a package at any time.
  • npm-nodemon
Areal world example:

It’s common to have commands that you run in order to do things like start your application, run code linting, or execute your applications test suite. Often these commands can get long and detailed with lots of options with vague values making them hard to remember. Not to mention, the need for everyone on the team to run the command with the same set of options or risk all kinds of conflicts.

Rather than make everyone remember exactly how to invoke the command to start the application, and to help speed up onboarding for people to the project, it would be convenient to provide an easy and memorable way for someone to start the application. This is where npm scripts come in handy.

Example package.json entry:

...
"scripts": {
	"start": "node index.js"
},
...

A common npm start script is something like node index.js. And maybe your project needs some additional flags to be set, for example: node index.js –port=8000 –debug=false.

Restart a Node.js Application upon Changing a File

Now we will see how to restart a Node.js application after changing a file, using an npm Script and Nodemon.

  • We can create a custom npm script to run our project during development.
  • let’s create the dev script which will reload our application on file changes during development. We will use nodemon’s –exec flag to tell nodemon to run an npm script whenever files in the project have changed.

Create a new "dev" entry in the scripts field of your package.json:

"scripts": {
	"start": "node index.js",
	"dev": "nodemon --exec 'npm start'"
}

Here we’ve told nodemon to execute the start script, instead of just running a file. Running nodemon without any commands would also start our index.js file, because nodemon will look for an index.js file by default and run that. We are telling nodemon to run the start script so that if we need to change how the application is started at any point, we only have to make the change to the start script.

Now that our scripts are all set up, run the dev script by typing the following into your terminal:

npm run dev

You should see nodemon start the application. If you edit and save a file in the project, you will see nodemon restart the application.

For more explanation visit:

Resources: