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