Debugger

  1. Stability: 3 - Stable

V8 comes with an extensive debugger which is accessible out-of-process via a
simple TCP protocol.
Node has a built-in client for this debugger. To use this, start Node with the
debug argument; a prompt will appear:

  1. % node debug myscript.js
  2. < debugger listening on port 5858
  3. connecting... ok
  4. break in /home/indutny/Code/git/indutny/myscript.js:1
  5. 1 x = 5;
  6. 2 setTimeout(function () {
  7. 3 debugger;
  8. debug>

Node’s debugger client doesn’t support the full range of commands, but
simple step and inspection is possible. By putting the statement debugger;
into the source code of your script, you will enable a breakpoint.

For example, suppose myscript.js looked like this:

  1. // myscript.js
  2. x = 5;
  3. setTimeout(function () {
  4. debugger;
  5. console.log("world");
  6. }, 1000);
  7. console.log("hello");

Then once the debugger is run, it will break on line 4.

  1. % node debug myscript.js
  2. < debugger listening on port 5858
  3. connecting... ok
  4. break in /home/indutny/Code/git/indutny/myscript.js:1
  5. 1 x = 5;
  6. 2 setTimeout(function () {
  7. 3 debugger;
  8. debug> cont
  9. < hello
  10. break in /home/indutny/Code/git/indutny/myscript.js:3
  11. 1 x = 5;
  12. 2 setTimeout(function () {
  13. 3 debugger;
  14. 4 console.log("world");
  15. 5 }, 1000);
  16. debug> next
  17. break in /home/indutny/Code/git/indutny/myscript.js:4
  18. 2 setTimeout(function () {
  19. 3 debugger;
  20. 4 console.log("world");
  21. 5 }, 1000);
  22. 6 console.log("hello");
  23. debug> repl
  24. Press Ctrl + C to leave debug repl
  25. > x
  26. 5
  27. > 2+2
  28. 4
  29. debug> next
  30. < world
  31. break in /home/indutny/Code/git/indutny/myscript.js:5
  32. 3 debugger;
  33. 4 console.log("world");
  34. 5 }, 1000);
  35. 6 console.log("hello");
  36. 7
  37. debug> quit
  38. %

The repl command allows you to evaluate code remotely. The next command
steps over to the next line. There are a few other commands available and more
to come. Type help to see others.

Watchers

You can watch expression and variable values while debugging your code.
On every breakpoint each expression from the watchers list will be evaluated
in the current context and displayed just before the breakpoint’s source code
listing.

To start watching an expression, type watch("my_expression"). watchers
prints the active watchers. To remove a watcher, type
unwatch("my_expression").

Commands reference

Stepping

  • 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)

Breakpoints

  • setBreakpoint(), sb() - Set breakpoint on current line
  • setBreakpoint(line), sb(line) - Set breakpoint on specific line
  • setBreakpoint('fn()'), sb(...) - Set breakpoint on a first statement in
    functions body
  • setBreakpoint('script.js', 1), sb(...) - Set breakpoint on first line of
    script.js
  • clearBreakpoint, cb(...) - Clear breakpoint

It is also possible to set a breakpoint in a file (module) that
isn’t loaded yet:

  1. % ./node debug test/fixtures/break-in-module/main.js
  2. < debugger listening on port 5858
  3. connecting to port 5858... ok
  4. break in test/fixtures/break-in-module/main.js:1
  5. 1 var mod = require('./mod.js');
  6. 2 mod.hello();
  7. 3 mod.hello();
  8. debug> setBreakpoint('mod.js', 23)
  9. Warning: script 'mod.js' was not loaded yet.
  10. 1 var mod = require('./mod.js');
  11. 2 mod.hello();
  12. 3 mod.hello();
  13. debug> c
  14. break in test/fixtures/break-in-module/mod.js:23
  15. 21
  16. 22 exports.hello = function() {
  17. 23 return 'hello from module';
  18. 24 };
  19. 25
  20. debug>

Info

  • backtrace, bt - Print backtrace of current execution frame
  • list(5) - List scripts source code with 5 line context (5 lines before and
    after)
  • watch(expr) - Add expression to watch list
  • unwatch(expr) - Remove expression from watch list
  • watchers - List all watchers and their values (automatically listed on each
    breakpoint)
  • repl - Open debugger’s repl for evaluation in debugging script’s context

Execution control

  • run - Run script (automatically runs on debugger’s start)
  • restart - Restart script
  • kill - Kill script

Various

  • scripts - List all loaded scripts
  • version - Display v8’s version

Advanced Usage

The V8 debugger can be enabled and accessed either by starting Node with
the --debug command-line flag or by signaling an existing Node process
with SIGUSR1.

Once a process has been set in debug mode with this it can be connected to
with the node debugger. Either connect to the pid or the URI to the debugger.
The syntax is:

  • node debug -p <pid> - Connects to the process via the pid
  • node debug <URI> - Connects to the process via the URI such as localhost:5858