• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Debugger
2
3<!--introduced_in=v0.9.12-->
4
5> Stability: 2 - Stable
6
7<!-- type=misc -->
8
9Node.js includes an out-of-process debugging utility accessible via a
10[V8 Inspector][] and built-in debugging client. To use it, start Node.js
11with the `inspect` argument followed by the path to the script to debug; a
12prompt will be displayed indicating successful launch of the debugger:
13
14```console
15$ node inspect myscript.js
16< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
17< For help, see: https://nodejs.org/en/docs/inspector
18< Debugger attached.
19Break on start in myscript.js:1
20> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
21  2 setTimeout(() => {
22  3   console.log('world');
23debug>
24```
25
26The Node.js debugger client is not a full-featured debugger, but simple step and
27inspection are possible.
28
29Inserting the statement `debugger;` into the source code of a script will
30enable a breakpoint at that position in the code:
31
32<!-- eslint-disable no-debugger -->
33```js
34// myscript.js
35global.x = 5;
36setTimeout(() => {
37  debugger;
38  console.log('world');
39}, 1000);
40console.log('hello');
41```
42
43Once the debugger is run, a breakpoint will occur at line 3:
44
45```console
46$ node inspect myscript.js
47< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
48< For help, see: https://nodejs.org/en/docs/inspector
49< Debugger attached.
50Break on start in myscript.js:1
51> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
52  2 setTimeout(() => {
53  3   debugger;
54debug> cont
55< hello
56break in myscript.js:3
57  1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
58  2 setTimeout(() => {
59> 3   debugger;
60  4   console.log('world');
61  5 }, 1000);
62debug> next
63break in myscript.js:4
64  2 setTimeout(() => {
65  3   debugger;
66> 4   console.log('world');
67  5 }, 1000);
68  6 console.log('hello');
69debug> repl
70Press Ctrl + C to leave debug repl
71> x
725
73> 2 + 2
744
75debug> next
76< world
77break in myscript.js:5
78  3   debugger;
79  4   console.log('world');
80> 5 }, 1000);
81  6 console.log('hello');
82  7
83debug> .exit
84```
85
86The `repl` command allows code to be evaluated remotely. The `next` command
87steps to the next line. Type `help` to see what other commands are available.
88
89Pressing `enter` without typing a command will repeat the previous debugger
90command.
91
92## Watchers
93
94It is possible to watch expression and variable values while debugging. On
95every breakpoint, each expression from the watchers list will be evaluated
96in the current context and displayed immediately before the breakpoint's
97source code listing.
98
99To begin watching an expression, type `watch('my_expression')`. The command
100`watchers` will print the active watchers. To remove a watcher, type
101`unwatch('my_expression')`.
102
103## Command reference
104
105### Stepping
106
107* `cont`, `c`: Continue execution
108* `next`, `n`: Step next
109* `step`, `s`: Step in
110* `out`, `o`: Step out
111* `pause`: Pause running code (like pause button in Developer Tools)
112
113### Breakpoints
114
115* `setBreakpoint()`, `sb()`: Set breakpoint on current line
116* `setBreakpoint(line)`, `sb(line)`: Set breakpoint on specific line
117* `setBreakpoint('fn()')`, `sb(...)`: Set breakpoint on a first statement in
118functions body
119* `setBreakpoint('script.js', 1)`, `sb(...)`: Set breakpoint on first line of
120`script.js`
121* `clearBreakpoint('script.js', 1)`, `cb(...)`: Clear breakpoint in `script.js`
122on line 1
123
124It is also possible to set a breakpoint in a file (module) that
125is not loaded yet:
126
127```console
128$ node inspect main.js
129< Debugger listening on ws://127.0.0.1:9229/4e3db158-9791-4274-8909-914f7facf3bd
130< For help, see: https://nodejs.org/en/docs/inspector
131< Debugger attached.
132Break on start in main.js:1
133> 1 (function (exports, require, module, __filename, __dirname) { const mod = require('./mod.js');
134  2 mod.hello();
135  3 mod.hello();
136debug> setBreakpoint('mod.js', 22)
137Warning: script 'mod.js' was not loaded yet.
138debug> c
139break in mod.js:22
140 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
141 21
142>22 exports.hello = function() {
143 23   return 'hello from module';
144 24 };
145debug>
146```
147
148### Information
149
150* `backtrace`, `bt`: Print backtrace of current execution frame
151* `list(5)`: List scripts source code with 5 line context (5 lines before and
152after)
153* `watch(expr)`: Add expression to watch list
154* `unwatch(expr)`: Remove expression from watch list
155* `watchers`: List all watchers and their values (automatically listed on each
156breakpoint)
157* `repl`: Open debugger's repl for evaluation in debugging script's context
158* `exec expr`: Execute an expression in debugging script's context
159
160### Execution control
161
162* `run`: Run script (automatically runs on debugger's start)
163* `restart`: Restart script
164* `kill`: Kill script
165
166### Various
167
168* `scripts`: List all loaded scripts
169* `version`: Display V8's version
170
171## Advanced usage
172
173### V8 inspector integration for Node.js
174
175V8 Inspector integration allows attaching Chrome DevTools to Node.js
176instances for debugging and profiling. It uses the
177[Chrome DevTools Protocol][].
178
179V8 Inspector can be enabled by passing the `--inspect` flag when starting a
180Node.js application. It is also possible to supply a custom port with that flag,
181e.g. `--inspect=9222` will accept DevTools connections on port 9222.
182
183To break on the first line of the application code, pass the `--inspect-brk`
184flag instead of `--inspect`.
185
186```console
187$ node --inspect index.js
188Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
189For help, see: https://nodejs.org/en/docs/inspector
190```
191
192(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
193at the end of the URL is generated on the fly, it varies in different
194debugging sessions.)
195
196If the Chrome browser is older than 66.0.3345.0,
197use `inspector.html` instead of `js_app.html` in the above URL.
198
199Chrome DevTools doesn't support debugging [worker threads][] yet.
200[ndb][] can be used to debug them.
201
202[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
203[V8 Inspector]: #debugger_v8_inspector_integration_for_node_js
204[worker threads]: worker_threads.html
205[ndb]: https://github.com/GoogleChromeLabs/ndb/
206