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`, `cb(...)` - Clear breakpoint
112 It is also possible to set a breakpoint in a file (module) that
115 % ./node debug test/fixtures/break-in-module/main.js
116 < debugger listening on port 5858
117 connecting to port 5858... ok
118 break in test/fixtures/break-in-module/main.js:1
119 1 var mod = require('./mod.js');
122 debug> setBreakpoint('mod.js', 23)
123 Warning: script 'mod.js' was not loaded yet.
124 1 var mod = require('./mod.js');
128 break in test/fixtures/break-in-module/mod.js:23
130 22 exports.hello = function() {
131 23 return 'hello from module';
138 * `backtrace`, `bt` - Print backtrace of current execution frame
139 * `list(5)` - List scripts source code with 5 line context (5 lines before and
141 * `watch(expr)` - Add expression to watch list
142 * `unwatch(expr)` - Remove expression from watch list
143 * `watchers` - List all watchers and their values (automatically listed on each
145 * `repl` - Open debugger's repl for evaluation in debugging script's context
147 ### Execution control
149 * `run` - Run script (automatically runs on debugger's start)
150 * `restart` - Restart script
151 * `kill` - Kill script
155 * `scripts` - List all loaded scripts
156 * `version` - Display v8's version
160 The V8 debugger can be enabled and accessed either by starting Node with
161 the `--debug` command-line flag or by signaling an existing Node process
164 Once a process has been set in debug mode with this it can be connected to
165 with the node debugger. Either connect to the `pid` or the URI to the debugger.
168 * `node debug -p <pid>` - Connects to the process via the `pid`
169 * `node debug <URI>` - Connects to the process via the URI such as localhost:5858