Introduction to debugging NodeJS code

In this blog we are going to see, how to use the debugging using node js code.
Node JS has inbuilt debugger functionalities.

To use that, in the command promt type node debug theNodeJsScript.js.
This debugger client is not a full feature one, but simple step and inspection are possible.

Lets have a small program as an example for debugging the code, this can be saved in debugging.js file

var i = 0;
for(;i>-10 ; i--){
console.log("value of i "+ i)
for(;i<0 ; i++){
console.log("value of i "+ i)

To debug use the command, “node debug server.js”

one can use the following command refference for stepping the code,

cont || c -> continue execution
next || n - > step next
step || s -> step in 
out || o -> step out
pause - pause the running code

You can use breakpoints in Node JS application, by using the following commands

setBreakPoint(), sb() // Set  breakpoint on current line
setBreakPoint(line), sb(line) // Set breakpoint on specific linenumber
setBreakPoint('function()'), sb('function()') // Set breakPoint on a first statement in function body
setBreakPoint('debugging.js', 1), sb('debugging.js', 1) // Set breakPoint on first line of debugging.jsc

The breakpoints can be cleared by using the following method,

clearBreakPoint('debugging.js', 1) - ClearBreakPoint in debugging.js in line one.

Some of the extra commands that could be used as well.

backtrace, bt -  print backtrace of current execution frame.
list(5) – list the script source code with 5 line context ( 5 lines before and 5 lines after)
watch(expression)- add some expression in watch list
unwatch(expression) – remove expression to watch list
watchers – list all thier watchers and their values
repl – Open Debugger's repl for evaluation in debugging script's context
exec expr – Execute an expression in debugging scripts context

I have also furnished some of the execution controls herein,

run - 	run script (automatically runs on debugger's start)
restart – restart the script
kill – kill the script

This commands works on node v6.10.3

Article Refference :

Happy Debugging!!!