Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
InvestmentNovel

Latest Programming Tutorial Blog

Nodemon Tutorial Example From Scratch

/
/
/
459 Views

Nodemon Tutorial Example From Scratch is the topic, we will discuss today. So if you are working on the Node.js application and getting tired of restarting the process every time you make a change? You can use nodemon in your Node app, watches the file system for changes and automatically restarts the process. Nodemon is the tool that helps you to develop node.js based applications by automatically restart a node application when file changes in the directory are detected. The nodemon server does not require any additional changes into your code or method of development. The nodemon is a replacement wrapper for the node, to use the nodemon replaces the word node on the command line when executing your script.

Nodemon Tutorial Example From Scratch

Install the nodemon server either globally or locally to your project using npm or Yarn:

yarn global add nodemon

# or

npm install nodemon -g

You can also install it locally per project using the following command.

yarn add nodemon --dev

# or

npm install nodemon --save-dev

#1: Create a Node.js project

Create a project folder using the following command.

mkdir nodemon-exp

Now, initialize the package.json file using the following command.

npm init -y

Okay, now install the express using the following command.

yarn add express

# or

npm install express --save

Create one file called server.js inside the root directory.

// server.js

const app = require('express')();

app.use('/', (req, res) => {
  res.status(200).send('Hello World!');
});

app.listen(5000);

Now, start the server using the following command.

nodemon server

 

Nodemon Tutorial Example From Scratch

Now, if you change inside the server.js file, the server will automatically restart, and you can get the latest output on the browser.

Alternatively, nodemon will also look for the primary file specified in your project’s package.json file or the start script. Given either of the examples below, you can then directly call nodemon to start your app in watch mode. We can also write the command in the start script in the package.json file.

{
  "name": "nodemon-exp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3"
  }
}

Now, you need to hit the following command.

npm start

It will do the same thing. 

#Advanced Options

There are a collection of options available, and you can access the summary of the available options with a following command.

nodemon -h

 

nodemon tutorial 2018

Okay, let me explain the fewer options.

  • --exec: Use –exec to specify the binary to execute the file with. For example, when combined with the ts-node binary, --exec can become useful to watch for changes and run TypeScript files.
  • --ext: Specify different file extensions to watch. For this switch, provide the comma-separated list of file extensions (e.g.: --ext js,ts).
  • --delay: By default nodemon waits for 1 second to restart a process when the file changes, but with --delay you can specify a different delay. For example, $ nodemon --delay 3.2for a 3.2 second delay.
  • --watch: Use the --watch switch to determine multiple directories or files to watch. Add one --watch switch for each list you want to watch. By default the current directory and its subdirectories are viewed, so with --watch you can narrow that to only specific subdirectories or files.
  • --ignore: Use --ignore to ignore particular files, file patterns or directories.
  • --verbose:  More verbose output with an information about what file(s) changed to trigger a restart.

#Nodemon Config

Adding configuration switches when running nodemon can get quite complicated. The better solution for the projects that need specific configurations is to specify these configurations in the nodemon.json file.

{
  "watch": ["server"],
  "ext": "ts",
  "ignore": ["*.test.ts"],
  "delay": "3",
  "execMap": {
    "ts": "ts-node"
  }
}

Alternatively, if you would instead not add the nodemon.json config file to your project, you can add these configurations to a package.json file under a nodemonConfig key:

{
  "name": "nodemon-exp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3"
  },
  "nodemonConfig": {
    "watch": [
      "server"
    ],
    "ext": "ts",
    "ignore": [
      "*.test.ts"
    ],
    "delay": "3",
    "execMap": {
      "ts": "ts-node"
    }
  }
}

To stop the nodemon process, use Ctrl + CNodemon emits events upon every change in state; start, restart crash, etc. You can add a nodemon configuration file nodemon.json.

The Nodemon wraps your application, so you can pass all the arguments you would usually give to your app. For example, if my application accepted a host and port as the arguments, I would start it as so:

nodemon ./server.js localhost 8080

Any output from the above script is prefixed with nodemonOtherwise, all output from your application, errors included, will be dumped out as expected. The nodemon also supports running and monitoring coffee-script apps:

nodemon server.coffee

If no script is given, then nodemon will test for the package.json file and if found, will run the file associated with a primary property. You can also pass a debug flag to node through a command line as you would normally:

nodemon --debug server.js 80

If you have the package.json file for your app, you can omit the main script entirely, and nodemon will read a package.json for a main property and use that value as the app.

Finally, Nodemon Tutorial Example From Scratch is over.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This div height required for enabling the sticky sidebar