Bootstrap FreeKB - Node.js - Getting Started with nodemon
Node.js - Getting Started with nodemon

Updated:   |  Node.js articles

nodemon is similar to the node CLI in that it is used to start or run a Node.js app. However, nodemon "monitors" the .js, .mjs, .coffee, .litcoffee, and .json files that make up your Node.js app and when a change is made to a .js, .mjs, .coffee, .litcoffee, or .json file, nodemon will restart your Node.js app.

This assumes you have the npm (Node.js Package Manager) CLI installed. If not, check out my article FreeKB - Node.js - Install Node.js on Linux.

The npm install command can be used to install the nodemon CLI in your present working directory.

npm install nodemon

 

However, it is more common to include the -g or --global flag to install nodemon in the node_modules directory of your Node.js installation so that nodemon can be used by any Node.js app on your system.

npm install --global nodemon

 

For example, if the base directory of your Node.js installation is /usr/local/nodejs/node-v20.10.0-linux-x64 then the nodemon CLI would be located at /usr/local/nodejs/node-v20.10.0-linux-x64/lib/node_modules/nodemon/bin/nodemon.js. Almost always, the ln command with the -s flag (on a Linux System) is used to create a symbolic link from /usr/bin/nodemon so that the nodemon CLI is in your $PATH (on a Linux system).

/usr/local/nodejs/node-v20.10.0-linux-x64/lib/node_modules/nodemon/bin/nodemon.js /usr/bin/nodemon

 

And then the nodemon CLI can be used.

~]$ nodemon --version
3.1.4

 

Let's create a temporary directory.

mkdir /tmp/foo

 

And move into the temporary directory.

cd /tmp/foo

 

And create a file named app.js that contains the following.

console.log("Hello World");

 

And run app.js using nodemon. Notice the output mentions "waiting for changes before restart".

~]$ nodemon app.js
[nodemon] 3.1.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js`
Hello World
[nodemon] clean exit - waiting for changes before restart

 

Let's change app.js to have something else.

console.log("Goodbye World");

 

app.js should have automatically restarted. Cool!

]$ nodemon app.js
[nodemon] 3.1.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js`
Hello World
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Goodbye World
[nodemon] clean exit - waiting for changes before restart

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 690671 in the box below so that we can be sure you are a human.