JavaScript

How to Execute the Start Script with Nodemon?

In Node.js the “start script” is the field of the “package.json” file which contains all the script commands that will be required to perform a particular operation. It is also known as the startup script of an application that allows the users to execute the targeted file without specifying its execution command in the configuration file. Moreover, the user can modify it at any time according to the application’s requirements due to its dynamic nature.

Quick Outline

Let’s first start with the basics of nodemon in Node.js.

What is Nodemon in Node.js?

Nodemon is the command line tool that helps in the fast deployment of the Node.js application. It monitors the Node.js application root directory continuously and restarts it automatically if any changes occur in it. This feature saves the time taken to stop and restart the Node.js application manually.

How to Install Nodemon in Node.js?

Nodemon is a third-party command line tool that the user needs to install in the Node.js application before executing the start script through it.

To do so, open the terminal(Ctrl+Shift+`) in the root directory of the Node.js application and execute the below-stated “npm(node package manager)” installation command:

npm install nodemon

The above command is executed successfully by adding the “nodemon” into the current Node.js application:

Check “nodemon” Version

For further verification, that the “nodemon” is successfully installed in Node.js or not run the below-stated version command:

nodemon -v

In the above-written command, the “-v” flag shows the “version” keyword.

The output shows that the “nodemon” is installed successfully having “3.0.1” version:

Global Installation of Nodemon

Apart from a particular Node.js application, the “nodemon” can be installed globally by specifying the “-g” flag along with the “npm” installation command and executing it in the operating system CMD:

npm i -g nodemon

Now the “nodemon” is installed globally for all Node.js applications in the current Windows operating system:

Once the installation is done, let’s jump into the use of it to execute the Node.js start script.

How to Execute the Start Script with Nodemon?

This section lists all possible approaches to execute the Node.js start script with “nodemon”:

Let’s start with the first approach which is the “nodemon” keyword.

Approach 1: Using “nodemon” Keyword

If the “nodemon” is installed globally in an operating system then the user can directly execute the Node.js start script without modifying the “script” section of the “package.json” file. To do so, simply type the “nodemon” keyword prepending with the file name in this way:

nodemon index.js

The output shows that the Node.js start script executed successfully:

Approach 2: Using Built-in Keys

Another way to execute the start script with “nodemon” is through the built-in key which is “npm start”. This command executes the defined file in the “start” key with “nodemon” without specifying the execution command.

For this purpose, first, navigate to the “package.json” file and add the “start” key along with its corresponding value into the “scripts” section:

"start": "nodemon ./index.js"

Press Ctrl+S to save the file after modification.

Now execute the below-stated command:

npm start

It can be analyzed that the file specified as a value of the “start” key has been executed successfully and now the Node.js application is in production mode because of the “start” keyword:

Approach 3: Using Custom Keys

The “start” script can be executed with “nodemon” by specifying it along with the targeted source code file name and location as a value of the custom key. Here, the custom key name is “dev” which specifies that Node.js application is in development mode:

"dev": "nodemon ./index.js"

Save the above file.

Now execute the below-stated “npm ” command. It will deploy the Node.js application in development mode:

npm run dev

It can be observed that the start script has been executed with “nodemon”:

How to Execute the Start Script With Nodemon Without Local Installation?

The working of the “nodemon” is not affected due to its local or global installation. Suppose the user uninstalls the “nodemon” locally from a specific Node.js application and executes the script through it then no error will occur. This is because if it is installed globally in an operating system then it can be accessed in any specific Node.js application without the local installation.

Let’s see the practical demonstration of the above-discussed scenario. For this purpose uninstall the “nodemon” from the current Node.js application using the below-stated command:

npm uninstall nodemon

The output shows that the “nodemon” has been uninstalled from the current Node.js application:

Now specify the “npx(Node Package eXecute)” NPM package runner along with the “nodemon” execution command to execute the “start” script:

npx nodemon index.js

It can be observed that the “nodemon” executed the “start” script successfully:

How to Automatically Restart Node.js Application?

Apart from starting the script, the “nodemon” tool can also be used to restart the application automatically after making changes to it. For example, if the user makes any changes in the source code of the Node.js application the “nodemon” restarts it automatically and shows the new changes.

For practical demonstration read our detailed guide on “How to Automatically Restart Node.js Application”.

Conclusion

To execute the start script with “nodemon” the user needs to first install the “nodemon” in the Node.js application via the “npm/yarn” package manager. Once it is installed then specify it along with the source code file name as a “nodemon <filename>” command. In addition, the user can also perform this task via the built-in key “start” or the “custom” keys that specify the path of the source file along with the “nodemon” and then execute that script via the “npm run <fieldname>” command. This post has covered all possible approaches to execute a strat script with nodemon.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.