7 V8 comes with an extensive debugger which is accessible out-of-process via a
8 simple [TCP protocol](http://code.google.com/p/v8/wiki/DebuggerProtocol).
9 Node has a built-in client for this debugger. To use this, start Node with the
10 `debug` argument; a prompt will appear:
12 % node debug myscript.js
13 < debugger listening on port 5858
15 break in /home/indutny/Code/git/indutny/myscript.js:1
17 2 setTimeout(function () {
21 Node's debugger client doesn't support the full range of commands, but
22 simple step and inspection is possible. By putting the statement `debugger;`
23 into the source code of your script, you will enable a breakpoint.
25 For example, suppose `myscript.js` looked like this:
29 setTimeout(function () {
35 Then once the debugger is run, it will break on line 4.
37 % node debug myscript.js
38 < debugger listening on port 5858
40 break in /home/indutny/Code/git/indutny/myscript.js:1
42 2 setTimeout(function () {
46 break in /home/indutny/Code/git/indutny/myscript.js:3
48 2 setTimeout(function () {
50 4 console.log("world");
53 break in /home/indutny/Code/git/indutny/myscript.js:4
54 2 setTimeout(function () {
56 4 console.log("world");
58 6 console.log("hello");
60 Press Ctrl + C to leave debug repl
67 break in /home/indutny/Code/git/indutny/myscript.js:5
69 4 console.log("world");
71 6 console.log("hello");
77 The `repl` command allows you to evaluate code remotely. The `next` command
78 steps over to the next line. There are a few other commands available and more
79 to come. Type `help` to see others.
83 You can watch expression and variable values while debugging your code.
84 On every breakpoint each expression from the watchers list will be evaluated
85 in the current context and displayed just before the breakpoint's source code
88 To start watching an expression, type `watch("my_expression")`. `watchers`
89 prints the active watchers. To remove a watcher, type
90 `unwatch("my_expression")`.
96 * `cont`, `c` - Continue execution
97 * `next`, `n` - Step next
98 * `step`, `s` - Step in
99 * `out`, `o` - Step out
100 * `pause` - Pause running code (like pause button in Developer Tools)
104 * `setBreakpoint()`, `sb()` - Set breakpoint on current line
105 * `setBreakpoint(line)`, `sb(line)` - Set breakpoint on specific line
106 * `setBreakpoint('fn()')`, `sb(...)` - Set breakpoint on a first statement in
108 * `setBreakpoint('script.js', 1)`, `sb(...)` - Set breakpoint on first line of
110 * `clearBreakpoint('script.js', 1)`, `cb(...)` - Clear breakpoint in script.js
113 It is also possible to set a breakpoint in a file (module) that
116 % ./node debug test/fixtures/break-in-module/main.js
117 < debugger listening on port 5858
118 connecting to port 5858... ok
119 break in test/fixtures/break-in-module/main.js:1
120 1 var mod = require('./mod.js');
123 debug> setBreakpoint('mod.js', 23)
124 Warning: script 'mod.js' was not loaded yet.
125 1 var mod = require('./mod.js');
129 break in test/fixtures/break-in-module/mod.js:23
131 22 exports.hello = function() {
132 23 return 'hello from module';
139 * `backtrace`, `bt` - Print backtrace of current execution frame
140 * `list(5)` - List scripts source code with 5 line context (5 lines before and
142 * `watch(expr)` - Add expression to watch list
143 * `unwatch(expr)` - Remove expression from watch list
144 * `watchers` - List all watchers and their values (automatically listed on each
146 * `repl` - Open debugger's repl for evaluation in debugging script's context
148 ### Execution control
150 * `run` - Run script (automatically runs on debugger's start)
151 * `restart` - Restart script
152 * `kill` - Kill script
156 * `scripts` - List all loaded scripts
157 * `version` - Display v8's version
161 The V8 debugger can be enabled and accessed either by starting Node with
162 the `--debug` command-line flag or by signaling an existing Node process
165 Once a process has been set in debug mode with this it can be connected to
166 with the node debugger. Either connect to the `pid` or the URI to the debugger.
169 * `node debug -p <pid>` - Connects to the process via the `pid`
170 * `node debug <URI>` - Connects to the process via the URI such as localhost:5858