When it comes to debugging node applications you have a few options. Most people I know use their IDE’s debugger which does the work really well for them. The next option for people who don’t use IDE’s is usually node inspector, which does a pretty good job too. Not long ago I found out that Node comes with a command line built in debugger which I tried and I think is easy enough that may become my debugger of choice.

To start debugging an application just use the debug command before running it:

1
node debug app.js

When you do this your script will be loaded but the execution will stop at the first line. You will also get a debug> prompt where you can issue debugger commands.

You can use the debugger statement to add breakpoints to your app. Lets debug a little app:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function fibonacci(n) {
  var fib = [];
  fib[0] = 0;
  fib[1] = 1;
  for (var i = 2; i <= n; i++){
    fib[i] = fib[i - 1] + fib[i - 2];
  }
  return fib[n];
}

function factorial(n) {
  var f = [];
  if (n === 0 || n === 1) {
    return 1;
  }
  if (f[n] > 0) {
    return f[n];
  } else {
    f[n] = factorial(n-1) * n;
    return f[n];
  }
}

var a = fibonacci(6);
debugger; // This is a break point
var b = factorial(6);
var c = a + b;

console.log(c);

We can start the debugger using the command we just learned:

1
2
3
4
5
6
7
8
9
10
node debug app.js
< debugger listening on port 5858
connecting... ok
break in app.js:24
 22 }
 23
 24 var a = fibonacci(6);
 25 debugger;
 26 var b = factorial(6);
debug>

You can see that the app broke on the first line of code that is actually being executed(the functions are being defined but they are not being executed yet). At this point line 24 hasn’t been executed yet and the debugger is waiting for instructions. From here we can use the step(can be abreviated as s) command to step into the function that is executed in that line:

1
2
3
4
5
6
7
debug> s
break in app.js:2
  1 function fibonacci(n) {
  2   var fib = [];
  3   fib[0] = 0;
  4   fib[1] = 1;
debug>

Now we are on line two. We can take a few steps forward using the next(n) command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
debug> n
break in app.js:3
  1 function fibonacci(n) {
  2   var fib = [];
  3   fib[0] = 0;
  4   fib[1] = 1;
  5   for (var i = 2; i <= n; i++){
debug> n
break in app.js:4
  2   var fib = [];
  3   fib[0] = 0;
  4   fib[1] = 1;
  5   for (var i = 2; i <= n; i++){
  6     fib[i] = fib[i - 1] + fib[i - 2];
debug> n
break in app.js:5
  3   fib[0] = 0;
  4   fib[1] = 1;
  5   for (var i = 2; i <= n; i++){
  6     fib[i] = fib[i - 1] + fib[i - 2];
  7   }
debug>

Say at this time we want to know the contents of fib. We can do this using the repl(Read-Eval-Print-Loop) command:

1
2
3
4
5
debug> repl
Press Ctrl + C to leave debug repl
> fib
[ 0, 1 ]
debug>

From the repl you can execute any JavaScript you want from the context of the script you are debugging. To go back to the debugger prompt use Ctrl + C. We can use the cont(c) command to continue the execution of our script until the next breakpoint (our debugger statement on line 25):

1
2
3
4
5
6
7
8
debug> c
break in app.js:25
 23
 24 var a = fibonacci(6);
 25 debugger;
 26 var b = factorial(6);
 27 var c = a + b;
debug>

The last command that I find using a lot is out(o). Out will take you out of the current function context:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
break in app.js:25
 23
 24 var a = fibonacci(6);
 25 debugger;
 26 var b = factorial(6);
 27 var c = a + b;
debug> n
break in app.js:26
 24 var a = fibonacci(6);
 25 debugger;
 26 var b = factorial(6);
 27 var c = a + b;
 28
debug> s
break in app.js:12
 10
 11 function factorial(n) {
 12   var f = [];
 13   if (n === 0 || n === 1) {
 14     return 1;
debug> o
break in app.js:27
 25 debugger;
 26 var b = factorial(6);
 27 var c = a + b;
 28
 29 console.log(c);
debug> repl
Press Ctrl + C to leave debug repl
> b
720

Once you get used to the most common commands it is pretty useful to debug even large applications.

[ debugging  javascript  node  programming  ]
Error Handling in Rust
Introduction to HTML Canvas
React refs
Introduction to Next JS - React framework
Introduction to React JS