Thursday 1 August 2019

Introduction to debug package


'debug' is a tiny JavaScript debugging utility.

Install debug package
Step 1: Create a folder ‘debugDemo’

Step 2: Go inside ‘debugDemo’ folder and execute the command ‘npm init’
This command takes basic information from you and create package.json file.
C:\Users\Public\nodeExamples\debugDemo>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (debugdemo)
version: (1.0.0)
description: Demo on debug package
entry point: (index.js)
test command:
git repository:
keywords: debug, node
author: krishna
license: (ISC)
About to write to C:\Users\Public\nodeExamples\debugDemo\package.json:

{
  "name": "debugdemo",
  "version": "1.0.0",
  "description": "Demo on debug package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "debug",
    "node"
  ],
  "author": "krishna",
  "license": "ISC"
}


Is this OK? (yes)

It creates package.json file like below.


package.json
{
  "name": "debugdemo",
  "version": "1.0.0",
  "description": "Demo on debug package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "debug",
    "node"
  ],
  "author": "krishna",
  "license": "ISC"
}

Step 3: Install debug package by executing the command 'npm install debug'

My package.json file is changed by adding debug dependencies.


package.json
{
  "name": "debugdemo",
  "version": "1.0.0",
  "description": "Demo on debug package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "debug",
    "node"
  ],
  "author": "krishna",
  "license": "ISC",
  "dependencies": {
    "debug": "^4.1.0"
  }
}

Hello World application
Step 1: Pass your module name to the debug function.
var debug = require('debug')('myModule')

Step 2: Export the DEBUG variable to myModule.

Step 3: Log the messages using debug()
debug("Started debugging the applicaiton")


index.js
var debug = require('debug')('myModule')

debug("Started debugging the applicaiton")

How to run index.js?
C:/>set DEBUG=myModule


C:/>node index.js
You can even use regular expression ‘*’ to debug specific modules.

For example, the statement 'set DEBUG=worker*' enable all the debug message for the modules start with name ‘worker’.


index.js
const a = require('debug')('worker:a')
const b = require('debug')('worker:b')
const c = require('debug')('worker:c')
 
function workA() {
  a('doing lots of uninteresting work')
  setTimeout(workA, Math.random() * 1000)
}

function workB() {
  b('doing some work')
  setTimeout(workB, Math.random() * 2000)
}
 
function workC() {
  c('doing work')
  setTimeout(workC, Math.random() * 3000)
}

workA()
workB()
workC()


What it the ms printing in debug log?
It represents the time spent between one debug() call and the next.


index.js
const a = require('debug')('worker:a')

function workA() {
  a('doing lots of uninteresting work')
  setTimeout(workA, Math.random() * 10000)
}

workA()



No comments:

Post a Comment