7 Node.js includes a full-featured out-of-process debugging utility accessible
8 via a simple [TCP-based protocol][] and built-in debugging client. To use it,
9 start Node.js with the `debug` argument followed by the path to the script to
10 debug; a prompt will be displayed indicating successful launch of the debugger:
13 % node debug myscript.js
14 < debugger listening on port 5858
16 break in /home/indutny/Code/git/indutny/myscript.js:1
18 2 setTimeout(function () {
23 Node.js's debugger client does not yet support the full range of commands, but
24 simple step and inspection are possible.
26 Inserting the statement `debugger;` into the source code of a script will
27 enable a breakpoint at that position in the code.
29 For example, suppose `myscript.js` is written as:
34 setTimeout(function () {
41 Once the debugger is run, a breakpoint will occur at line 4:
44 % node debug myscript.js
45 < debugger listening on port 5858
47 break in /home/indutny/Code/git/indutny/myscript.js:1
49 2 setTimeout(function () {
53 break in /home/indutny/Code/git/indutny/myscript.js:3
55 2 setTimeout(function () {
57 4 console.log('world');
60 break in /home/indutny/Code/git/indutny/myscript.js:4
61 2 setTimeout(function () {
63 4 console.log('world');
65 6 console.log('hello');
67 Press Ctrl + C to leave debug repl
74 break in /home/indutny/Code/git/indutny/myscript.js:5
76 4 console.log('world');
78 6 console.log('hello');
84 The `repl` command allows code to be evaluated remotely. The `next` command
85 steps over to the next line. Type `help` to see what other commands are
90 It is possible to watch expression and variable values while debugging. On
91 every breakpoint, each expression from the watchers list will be evaluated
92 in the current context and displayed immediately before the breakpoint's
95 To begin watching an expression, type `watch('my_expression')`. The command
96 `watchers` will print the active watchers. To remove a watcher, type
97 `unwatch('my_expression')`.
103 * `cont`, `c` - Continue execution
104 * `next`, `n` - Step next
105 * `step`, `s` - Step in
106 * `out`, `o` - Step out
107 * `pause` - Pause running code (like pause button in Developer Tools)
111 * `setBreakpoint()`, `sb()` - Set breakpoint on current line
112 * `setBreakpoint(line)`, `sb(line)` - Set breakpoint on specific line
113 * `setBreakpoint('fn()')`, `sb(...)` - Set breakpoint on a first statement in
115 * `setBreakpoint('script.js', 1)`, `sb(...)` - Set breakpoint on first line of
117 * `clearBreakpoint('script.js', 1)`, `cb(...)` - Clear breakpoint in script.js
120 It is also possible to set a breakpoint in a file (module) that
124 % ./node debug test/fixtures/break-in-module/main.js
125 < debugger listening on port 5858
126 connecting to port 5858... ok
127 break in test/fixtures/break-in-module/main.js:1
128 1 var mod = require('./mod.js');
131 debug> setBreakpoint('mod.js', 23)
132 Warning: script 'mod.js' was not loaded yet.
133 1 var mod = require('./mod.js');
137 break in test/fixtures/break-in-module/mod.js:23
139 22 exports.hello = function() {
140 23 return 'hello from module';
148 * `backtrace`, `bt` - Print backtrace of current execution frame
149 * `list(5)` - List scripts source code with 5 line context (5 lines before and
151 * `watch(expr)` - Add expression to watch list
152 * `unwatch(expr)` - Remove expression from watch list
153 * `watchers` - List all watchers and their values (automatically listed on each
155 * `repl` - Open debugger's repl for evaluation in debugging script's context
157 ### Execution control
159 * `run` - Run script (automatically runs on debugger's start)
160 * `restart` - Restart script
161 * `kill` - Kill script
165 * `scripts` - List all loaded scripts
166 * `version` - Display V8's version
170 An alternative way of enabling and accessing the debugger is to start
171 Node.js with the `--debug` command-line flag or by signaling an existing
172 Node.js process with `SIGUSR1`.
174 Once a process has been set in debug mode this way, it can be connected to
175 using the Node.js debugger by either connecting to the `pid` of the running
176 process or via URI reference to the listening debugger:
178 * `node debug -p <pid>` - Connects to the process via the `pid`
179 * `node debug <URI>` - Connects to the process via the URI such as
182 [TCP-based protocol]: https://github.com/v8/v8/wiki/Debugging-Protocol