Search code examples
debuggingnode.jsgoogle-chrome-devtoolsweb-inspectornode-inspector

Node Inspector: Running Code - Not Just Stepping Through


What I want

I want to be able to run a whole script in node during debugging with node-inspector and Web Inspector. - I don't want to step through the individual JavaScript calls.

What I did

(My PowerShell Instructions)

PS C:\Users\JK> node-inspector
   info  - socket.io started
visit http://0.0.0.0:8080/debug?port=5858 to start debugging
...

==[In another PowerShell instance:]==

PS %> node —debug-brk myscript.js
debugger listening on port 5858

Why I want that

I'm writing a node script. In this script I console.log a lot of objects in order to be able to explore them during the debugging process. But the simple static textual console output isn't really nice - You can't fold and expand your object's properties or get the source code of a function:

(For Example)

{ [Function: Xy]
  a: [Function],
  b: 8.2,
  c: [Function],
  d: [Circular],
  e: '2011-11-11' }

So I decided to use Web Inspector with node-inspector in order to get a good object browse experience (because of Web Inspector's nice output formatting).

Why I Don't Step Through

(Structure of My Script)

var fs = require('fs');

fs.readFile('myfile', function (err, data) {
    if (err) {
        throw err;
    }

    //My Script...

    console.log(something);
});
  1. The console.log() calls are executed in a callback function of require('fs').readFile(). I won't get there just with "normal" steps.
  2. It's simply boring the click the Step buttons again and again.

My Questions

  • Is there a possibility to run a script without stepping through using the following Web Inspector user interface? (I don't want to use node —debug myscript.js instead of node —debug-brk myscript.js because then Inspector throws Error: connect ECONNREFUSED Is node running with --debug port 5858? because the script runs too fast)

(Web Inspector Interface) Web Inspector Interface

  • Or is there at least any other way to do what I described above (in the Why I want that section).

Thanks. - (I hope it's clear what I wanted to ask. - Please write a comment if it isn't.)


Solution

  • You have a couple options.

    Using --debug-brk:

    1. Start your script, let it stop on the first line.

      In the Script pane, click the line number inside the callback (line 4 in this example).

      Click "Continue" (the "|> icon above the right-hand panel).

    Using --debug:

    1. Add the line debugger; to your callback. This will stop the debugger at that point. Click "|>" when you're done.