Debugging

Debugging a Node.js application is very similar to debugging any other application. IDEs like WebStorm or IntelliJ provide a traditional debugger where you can install breakpoints and stop the execution whenever the application hits the given line.

This is perfect if you buy a license for one of the IDEs, but there is a free alternative that will have a very similiar result for the users of Google Chrome, node-inspector.

Node-inspector is an npm package that pretty much enables the Chrome debugger to debug Node.js applications.

Let's see how it works:

  1. First of all, we need to install node-inspector:
    npm install –g node-inspector
    

    This should add a command to our system called node-inspector. If we execute it, we get the following output:

    Debugging

    That means our debug server has started.

  2. Now we need to run a node application with a special flag to indicate that it needs to be debugged.

    Let's take a simple Seneca act as an example:

    var seneca = require( 'seneca' )()
    seneca.add({role: 'math', cmd: 'sum'}, function (msg, respond) {
      var sum = msg.left + msg.right
      respond(null, {answer: sum})
    })
    
    seneca.add({role: 'math', cmd: 'product'}, function (msg, respond) {
      var product = msg.left * msg.right
      respond( null, { answer: product } )
    })
    
    
    seneca.act({role: 'math', cmd: 'sum', left: 1, right: 2}, console.log)
    seneca.act({role: 'math', cmd: 'product', left: 3, right: 4}, console.log)
  3. Now, in order to run it on the debug mode, execute the following command:
    node index.js --debug-brk
    

    The way to access the debugger is through the URL http://127.0.0.1:8080/?port=5858:

    Debugging

I am sure this image is very familiar to every developer in the world: it is the Chrome debugger showing our code. As you can see in the first line, the one highlighted in blue, the application stopped in the first instruction so that we can place the breakpoints by clicking the line numbers, as shown in the following image:

Debugging

As you can see in the preceding image, we have installed a breakpoint in line 9. Now we can use the control panel to navigate through the code and values of our variables:

Debugging

The controls on the top speak for themselves if you ever debugged an application:

  • The first button is called play and it allows the application to run to the next breakpoint
  • Step over executes the next line in the current file
  • Step into goes into the next line, getting deeper in the call stack so that we can see the call hierarchy
  • Step out is the reverse of step into
  • Disable breakpoints will prevent the program from stopping at the breakpoints
  • Pause on exceptions, as its name indicates, will cause the program to stop on exceptions (it is very useful when trying to catch errors)

If we click on play, we can see how the script will stop in line 9 in the following image:

Debugging

As a good debugger, it will let us inspect the value of our variables by hovering the cursor over the variable name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.29.71