Debugging

Node JS

NodeJS Debug Tools

node debugger

Node Debugger

launch with

node debug app.js

Example app.js

function floop (i) {
  if (i > 0) {
    var decrementer = 10;
    while( decrementer-- > 0 ){
      i--;
    }
    floop(i);
  }
  return i;
}

floop(50);

Debug app.js

node debug app.js 
< Debugger listening on port 5858
connecting to port 5858... ok
break in app.js:12
 10 }
 11 
>12 floop(50);
 13 });
debug> 

Stepping

  • cont - Continue execution
    alias c
  • next - Step next
    alias n
  • step - Step in
    alias s
  • out - Step out
    alias o
  • pause - Pause running code

Debugger Commands

Step Into the function

debug> s
break in app.js:2
  1 function floop (i) {
> 2   if (i > 0) {
  3     var decrementer = 10;
  4     while( decrementer-- > 0 ){

press s to step into the function

REPL

debug> repl
Press Ctrl + C to leave debug repl
> i
50
> i = 20;
20
> i
20

launch a repl using the command repl

ctrl + c goes back to debugging console

press s to continue stepping through

with the new value of i being 20

Watchers

debug> watch("decrementer");
watch("expr")
unwatch("expr")​

Watchers

debug> s
break in app.js:3
Watchers:
  0: decrementer = undefined

  1 function floop (i) {
  2   if (i > 0) {
> 3     var decrementer = 10;
  4     while( decrementer-- > 0 ){
  5       i--;

Step in
and the expression "decrementer" will be watched

Watchers

debug> s
break in app.js:4
Watchers:
  0: decrementer = 10

  2   if (i > 0) {
  3     var decrementer = 10;
> 4     while( decrementer-- > 0 ){
  5       i--;
  6     }

Keep stepping through and notice the watched epression

Watchers

debug> s
break in app.js:5
Watchers:
  0: decrementer = 9

  3     var decrementer = 10;
  4     while( decrementer-- > 0 ){
> 5       i--;
  6     }
  7     floop(i);

Keep stepping through and notice the watched epression

Unwatch Expression

unwatch("decrementer")

Setting Breakpoints

Set breakpoint on current line
setBreakpoint()

Set breakpoint on specific line
setBreakpoint(line #)

 

Set breakpoint on the first statement in function
setBreakpoint('fn()')

Set breakpoint on line n of script.js
setBreakpoint('script.js', 22)

alias : sb(args)

Set Breakpoint

debug> sb(7);
  1 function floop (i) {
  2   if (i > 0) {
  3     var decrementer = 10;
  4     while( decrementer-- > 0 ){
> 5       i--;
  6     }
* 7     floop(i);
  8   }
  9   return i;
 10 }

Continue

debug> c
break in app.js:7
  5       i--;
  6     }
> 7     floop(i);
  8   }
  9   return i;

c to continue

Clear Breakpoint

clearBreakpoint('app.js',7)
alias : cb(args)

Adding breakpoint 

with debugger;

debug> c
break in app.js:7
  5       i--;
  6     }
> 7     debugger;
  8     floop(i);
  9   }

Backtrace

Print backtrace of the stack

node debug app.js 
c
c
c
bt

backtrace

alias bt

#0 app.js:7:5
#1 app.js:8:5
#2 app.js:8:5
#3 app.js:13:1

debugger hanging?

keep pressing c and debugger will hang

kill with

killall node

manually exit from the process

add this to end of your script

process.exit();

Run and restart

press c until the process exits

debug> c
program terminated

run to start the terminated process

debug> run
< Debugger listening on port 5858
connecting to port 5858... ok

restart will restart the process

debug> restart
program terminated
< Debugger listening on port 5858
connecting to port 5858... ok

Debug Node in the browser

$ node --inspect index.js

Run it with the --inspect flag 

$ node --inspect --debug-brk index.js

You can also break on the first statement of the script with --debug-brk.

Open the provided URL

Resources

Made with Slides.com