Monday, June 9, 2014

Debug in node.js

A few days ago someone asked me if I knew how to debug in node.js and to be honest I was debugging with the console object, although it is effective I thought there should be better ways to debug a script, so I'm going to explain you my findings and hope to help someone that was in the same position as me.

So let's start with the simplest way to debug with the console object.


Save the script and run it, the console will output in each iteration the value of the variables used in this case "i" and "sum", like the following:

Iteration:0, sum:0
Iteration:1, sum:1
Iteration:2, sum:3
Iteration:3, sum:6
Iteration:4, sum:10
Iteration:5, sum:15
Iteration:6, sum:21
Iteration:7, sum:28
Iteration:8, sum:36
Iteration:9, sum:45

The console object has other methods besides log(), you can find more about it in the following link:

http://nodejs.org/api/stdio.html

Since V8 node comes with a debugger, it has a built in client for this debugger to use it just add the debug argument when start running the script. Save the previous script as scriptDebug.js then type the following command to start the debugger:

               node debug scriptDebug.js

After that you will see a prompt to the debugger client as the following:

< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
  1 var sum = 0;
  2
  3 for(i=0;i<10;i++) {
debug>

Now the debug client support some basic commands to debug the script, here are some of them:
  • cont, c - Continue execution
  • next, n - Step next
  • step, s - Step in
  • out, o - Step out
  • pause - Pause running code (like pause button in Developer Tools)
  • setBreakpoint(), sb() - Set breakpoint on current line
  • clearBreakpoint, cb(...) - Clear breakpoint
  • .....
In the debugg mode if you type the command cont as this:

< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
  1 var sum = 0;
  2
  3 for(i=0;i<10;i++) {
debug> cont
< Iteration:0, sum:0
< Iteration:1, sum:1
< Iteration:2, sum:3
< Iteration:3, sum:6
< Iteration:4, sum:10
< Iteration:5, sum:15
< Iteration:6, sum:21
< Iteration:7, sum:28
< Iteration:8, sum:36
< Iteration:9, sum:45
program terminated

You will see will be the output of the program, this is because the is no breakpoint in the code.

To set a break point you could use the command sb(), for example to set a break point in the line 5, it would be like this:

connecting... ok
break in scriptDebug.js:1
  1 var sum = 0;
  2
  3 for(i=0;i<10;i++) {
debug> sb(5);
  1 var sum = 0;
  2
  3 for(i=0;i<10;i++) {
  4 sum = sum + i;
* 5  console.log('Iteration:' + i +', sum:' + sum);
  6 }

Now every time a continue command is given it will stop in the breakpoint, as the following examples shows:

debug> cont
< Iteration:0, sum:0
break in scriptDebug.js:5
  3 for(i=0;i<10;i++) {
  4 sum = sum + i;
* 5  console.log('Iteration:' + i +', sum:' + sum);
  6 }
  
Another way to place a breakpoint is to set the debugger instruction in your code as the following example:



Once you can go through your code, what is missing is to inspect the values of the variables in your program, to do this let's use the watch command in the script we have used so far, the following example show how to use it:

$ node debug scriptDebug.js
< debugger listening on port 5858
connecting... ok
break in scriptDebug.js:1
  1 var sum = 0;
  2 
  3 for(i=0;i<10;i++) {
debug> watch('sum')
debug> cont
< Iteration:0, sum:0
break in scriptDebug.js:6
Watchers:
  0: sum = 0

  4 sum = sum + i;
  5  console.log('Iteration:' + i +', sum:' + sum);
  6  debugger;
  7 }
  8 
debug> cont
break in scriptDebug.js:6
< Iteration:1, sum:1
Watchers:
  0: sum = 1

  4 sum = sum + i;
  5  console.log('Iteration:' + i +', sum:' + sum);
  6  debugger;
  7 }
  8 

As you can see now every time a breakpoint is found the variable sum is printed with its value.

With this tool you can easily go through your code, stop wherever you want and do many more things, you can find all the instructions in the following link:

http://nodejs.org/api/debugger.html

The debbuger that comes with node.js is very powerful but still is a command based utility, there are debbugers with more visual help, lets use node inspector to have a visual aid to debug our script.

First lets install globally node inspector, with the help of npm use the following command:

        $ npm install -g node-inspector

With node inspector installed let's start it with the following command:

$ node-inspector
Node Inspector v0.7.3
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

With node inspector running in other terminal you have to start in debug mode your application with the following command:

$ node --debug-brk scriptDebug.js
debugger listening on port 5858

The --debug-brk option is a node inspector option which starts to debug the script and stops it in the first line.

Now let's open a browser with url indicated "http://127.0.0.1:8080/debug?port=5858" by node inspector, you will see the following console:


As you can see the script is displayed and in the right side there is a window that displays all the information about the runtime values of the script, as in the upper right side there are some navigation buttons to go through the script.

With node inspector the debugging of a script is made so much easier because it is a visual tool and all the commands are a click away.

You can find more about node inspector in the following link:
https://github.com/node-inspector/node-inspector

This is a brief example of how to start using the debug tools of node.js and I hope it is useful to you.