11cb0ef41Sopenharmony_ci# Debugger
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.9.12-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- type=misc -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciNode.js includes a command-line debugging utility. The Node.js debugger client
101cb0ef41Sopenharmony_ciis not a full-featured debugger, but simple stepping and inspection are
111cb0ef41Sopenharmony_cipossible.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciTo use it, start Node.js with the `inspect` argument followed by the path to the
141cb0ef41Sopenharmony_ciscript to debug.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci```console
171cb0ef41Sopenharmony_ci$ node inspect myscript.js
181cb0ef41Sopenharmony_ci< Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
191cb0ef41Sopenharmony_ci< For help, see: https://nodejs.org/en/docs/inspector
201cb0ef41Sopenharmony_ci<
211cb0ef41Sopenharmony_ciconnecting to 127.0.0.1:9229 ... ok
221cb0ef41Sopenharmony_ci< Debugger attached.
231cb0ef41Sopenharmony_ci<
241cb0ef41Sopenharmony_ci ok
251cb0ef41Sopenharmony_ciBreak on start in myscript.js:2
261cb0ef41Sopenharmony_ci  1 // myscript.js
271cb0ef41Sopenharmony_ci> 2 global.x = 5;
281cb0ef41Sopenharmony_ci  3 setTimeout(() => {
291cb0ef41Sopenharmony_ci  4   debugger;
301cb0ef41Sopenharmony_cidebug>
311cb0ef41Sopenharmony_ci```
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciThe debugger automatically breaks on the first executable line. To instead
341cb0ef41Sopenharmony_cirun until the first breakpoint (specified by a [`debugger`][] statement), set
351cb0ef41Sopenharmony_cithe `NODE_INSPECT_RESUME_ON_START` environment variable to `1`.
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci```console
381cb0ef41Sopenharmony_ci$ cat myscript.js
391cb0ef41Sopenharmony_ci// myscript.js
401cb0ef41Sopenharmony_ciglobal.x = 5;
411cb0ef41Sopenharmony_cisetTimeout(() => {
421cb0ef41Sopenharmony_ci  debugger;
431cb0ef41Sopenharmony_ci  console.log('world');
441cb0ef41Sopenharmony_ci}, 1000);
451cb0ef41Sopenharmony_ciconsole.log('hello');
461cb0ef41Sopenharmony_ci$ NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js
471cb0ef41Sopenharmony_ci< Debugger listening on ws://127.0.0.1:9229/f1ed133e-7876-495b-83ae-c32c6fc319c2
481cb0ef41Sopenharmony_ci< For help, see: https://nodejs.org/en/docs/inspector
491cb0ef41Sopenharmony_ci<
501cb0ef41Sopenharmony_ciconnecting to 127.0.0.1:9229 ... ok
511cb0ef41Sopenharmony_ci< Debugger attached.
521cb0ef41Sopenharmony_ci<
531cb0ef41Sopenharmony_ci< hello
541cb0ef41Sopenharmony_ci<
551cb0ef41Sopenharmony_cibreak in myscript.js:4
561cb0ef41Sopenharmony_ci  2 global.x = 5;
571cb0ef41Sopenharmony_ci  3 setTimeout(() => {
581cb0ef41Sopenharmony_ci> 4   debugger;
591cb0ef41Sopenharmony_ci  5   console.log('world');
601cb0ef41Sopenharmony_ci  6 }, 1000);
611cb0ef41Sopenharmony_cidebug> next
621cb0ef41Sopenharmony_cibreak in myscript.js:5
631cb0ef41Sopenharmony_ci  3 setTimeout(() => {
641cb0ef41Sopenharmony_ci  4   debugger;
651cb0ef41Sopenharmony_ci> 5   console.log('world');
661cb0ef41Sopenharmony_ci  6 }, 1000);
671cb0ef41Sopenharmony_ci  7 console.log('hello');
681cb0ef41Sopenharmony_cidebug> repl
691cb0ef41Sopenharmony_ciPress Ctrl+C to leave debug repl
701cb0ef41Sopenharmony_ci> x
711cb0ef41Sopenharmony_ci5
721cb0ef41Sopenharmony_ci> 2 + 2
731cb0ef41Sopenharmony_ci4
741cb0ef41Sopenharmony_cidebug> next
751cb0ef41Sopenharmony_ci< world
761cb0ef41Sopenharmony_ci<
771cb0ef41Sopenharmony_cibreak in myscript.js:6
781cb0ef41Sopenharmony_ci  4   debugger;
791cb0ef41Sopenharmony_ci  5   console.log('world');
801cb0ef41Sopenharmony_ci> 6 }, 1000);
811cb0ef41Sopenharmony_ci  7 console.log('hello');
821cb0ef41Sopenharmony_ci  8
831cb0ef41Sopenharmony_cidebug> .exit
841cb0ef41Sopenharmony_ci$
851cb0ef41Sopenharmony_ci```
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciThe `repl` command allows code to be evaluated remotely. The `next` command
881cb0ef41Sopenharmony_cisteps to the next line. Type `help` to see what other commands are available.
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciPressing `enter` without typing a command will repeat the previous debugger
911cb0ef41Sopenharmony_cicommand.
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci## Watchers
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciIt is possible to watch expression and variable values while debugging. On
961cb0ef41Sopenharmony_cievery breakpoint, each expression from the watchers list will be evaluated
971cb0ef41Sopenharmony_ciin the current context and displayed immediately before the breakpoint's
981cb0ef41Sopenharmony_cisource code listing.
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ciTo begin watching an expression, type `watch('my_expression')`. The command
1011cb0ef41Sopenharmony_ci`watchers` will print the active watchers. To remove a watcher, type
1021cb0ef41Sopenharmony_ci`unwatch('my_expression')`.
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci## Command reference
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci### Stepping
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci* `cont`, `c`: Continue execution
1091cb0ef41Sopenharmony_ci* `next`, `n`: Step next
1101cb0ef41Sopenharmony_ci* `step`, `s`: Step in
1111cb0ef41Sopenharmony_ci* `out`, `o`: Step out
1121cb0ef41Sopenharmony_ci* `pause`: Pause running code (like pause button in Developer Tools)
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci### Breakpoints
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci* `setBreakpoint()`, `sb()`: Set breakpoint on current line
1171cb0ef41Sopenharmony_ci* `setBreakpoint(line)`, `sb(line)`: Set breakpoint on specific line
1181cb0ef41Sopenharmony_ci* `setBreakpoint('fn()')`, `sb(...)`: Set breakpoint on a first statement in
1191cb0ef41Sopenharmony_ci  function's body
1201cb0ef41Sopenharmony_ci* `setBreakpoint('script.js', 1)`, `sb(...)`: Set breakpoint on first line of
1211cb0ef41Sopenharmony_ci  `script.js`
1221cb0ef41Sopenharmony_ci* `setBreakpoint('script.js', 1, 'num < 4')`, `sb(...)`: Set conditional
1231cb0ef41Sopenharmony_ci  breakpoint on first line of `script.js` that only breaks when `num < 4`
1241cb0ef41Sopenharmony_ci  evaluates to `true`
1251cb0ef41Sopenharmony_ci* `clearBreakpoint('script.js', 1)`, `cb(...)`: Clear breakpoint in `script.js`
1261cb0ef41Sopenharmony_ci  on line 1
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciIt is also possible to set a breakpoint in a file (module) that
1291cb0ef41Sopenharmony_ciis not loaded yet:
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci```console
1321cb0ef41Sopenharmony_ci$ node inspect main.js
1331cb0ef41Sopenharmony_ci< Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
1341cb0ef41Sopenharmony_ci< For help, see: https://nodejs.org/en/docs/inspector
1351cb0ef41Sopenharmony_ci<
1361cb0ef41Sopenharmony_ciconnecting to 127.0.0.1:9229 ... ok
1371cb0ef41Sopenharmony_ci< Debugger attached.
1381cb0ef41Sopenharmony_ci<
1391cb0ef41Sopenharmony_ciBreak on start in main.js:1
1401cb0ef41Sopenharmony_ci> 1 const mod = require('./mod.js');
1411cb0ef41Sopenharmony_ci  2 mod.hello();
1421cb0ef41Sopenharmony_ci  3 mod.hello();
1431cb0ef41Sopenharmony_cidebug> setBreakpoint('mod.js', 22)
1441cb0ef41Sopenharmony_ciWarning: script 'mod.js' was not loaded yet.
1451cb0ef41Sopenharmony_cidebug> c
1461cb0ef41Sopenharmony_cibreak in mod.js:22
1471cb0ef41Sopenharmony_ci 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
1481cb0ef41Sopenharmony_ci 21
1491cb0ef41Sopenharmony_ci>22 exports.hello = function() {
1501cb0ef41Sopenharmony_ci 23   return 'hello from module';
1511cb0ef41Sopenharmony_ci 24 };
1521cb0ef41Sopenharmony_cidebug>
1531cb0ef41Sopenharmony_ci```
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ciIt is also possible to set a conditional breakpoint that only breaks when a
1561cb0ef41Sopenharmony_cigiven expression evaluates to `true`:
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci```console
1591cb0ef41Sopenharmony_ci$ node inspect main.js
1601cb0ef41Sopenharmony_ci< Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
1611cb0ef41Sopenharmony_ci< For help, see: https://nodejs.org/en/docs/inspector
1621cb0ef41Sopenharmony_ci<
1631cb0ef41Sopenharmony_ciconnecting to 127.0.0.1:9229 ... ok
1641cb0ef41Sopenharmony_ci< Debugger attached.
1651cb0ef41Sopenharmony_ciBreak on start in main.js:7
1661cb0ef41Sopenharmony_ci  5 }
1671cb0ef41Sopenharmony_ci  6
1681cb0ef41Sopenharmony_ci> 7 addOne(10);
1691cb0ef41Sopenharmony_ci  8 addOne(-1);
1701cb0ef41Sopenharmony_ci  9
1711cb0ef41Sopenharmony_cidebug> setBreakpoint('main.js', 4, 'num < 0')
1721cb0ef41Sopenharmony_ci  1 'use strict';
1731cb0ef41Sopenharmony_ci  2
1741cb0ef41Sopenharmony_ci  3 function addOne(num) {
1751cb0ef41Sopenharmony_ci> 4   return num + 1;
1761cb0ef41Sopenharmony_ci  5 }
1771cb0ef41Sopenharmony_ci  6
1781cb0ef41Sopenharmony_ci  7 addOne(10);
1791cb0ef41Sopenharmony_ci  8 addOne(-1);
1801cb0ef41Sopenharmony_ci  9
1811cb0ef41Sopenharmony_cidebug> cont
1821cb0ef41Sopenharmony_cibreak in main.js:4
1831cb0ef41Sopenharmony_ci  2
1841cb0ef41Sopenharmony_ci  3 function addOne(num) {
1851cb0ef41Sopenharmony_ci> 4   return num + 1;
1861cb0ef41Sopenharmony_ci  5 }
1871cb0ef41Sopenharmony_ci  6
1881cb0ef41Sopenharmony_cidebug> exec('num')
1891cb0ef41Sopenharmony_ci-1
1901cb0ef41Sopenharmony_cidebug>
1911cb0ef41Sopenharmony_ci```
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci### Information
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci* `backtrace`, `bt`: Print backtrace of current execution frame
1961cb0ef41Sopenharmony_ci* `list(5)`: List scripts source code with 5 line context (5 lines before and
1971cb0ef41Sopenharmony_ci  after)
1981cb0ef41Sopenharmony_ci* `watch(expr)`: Add expression to watch list
1991cb0ef41Sopenharmony_ci* `unwatch(expr)`: Remove expression from watch list
2001cb0ef41Sopenharmony_ci* `unwatch(index)`: Remove expression at specific index from watch list
2011cb0ef41Sopenharmony_ci* `watchers`: List all watchers and their values (automatically listed on each
2021cb0ef41Sopenharmony_ci  breakpoint)
2031cb0ef41Sopenharmony_ci* `repl`: Open debugger's repl for evaluation in debugging script's context
2041cb0ef41Sopenharmony_ci* `exec expr`, `p expr`: Execute an expression in debugging script's context and
2051cb0ef41Sopenharmony_ci  print its value
2061cb0ef41Sopenharmony_ci* `profile`: Start CPU profiling session
2071cb0ef41Sopenharmony_ci* `profileEnd`: Stop current CPU profiling session
2081cb0ef41Sopenharmony_ci* `profiles`: List all completed CPU profiling sessions
2091cb0ef41Sopenharmony_ci* `profiles[n].save(filepath = 'node.cpuprofile')`: Save CPU profiling session
2101cb0ef41Sopenharmony_ci  to disk as JSON
2111cb0ef41Sopenharmony_ci* `takeHeapSnapshot(filepath = 'node.heapsnapshot')`: Take a heap snapshot
2121cb0ef41Sopenharmony_ci  and save to disk as JSON
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci### Execution control
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci* `run`: Run script (automatically runs on debugger's start)
2171cb0ef41Sopenharmony_ci* `restart`: Restart script
2181cb0ef41Sopenharmony_ci* `kill`: Kill script
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci### Various
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci* `scripts`: List all loaded scripts
2231cb0ef41Sopenharmony_ci* `version`: Display V8's version
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci## Advanced usage
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci### V8 inspector integration for Node.js
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ciV8 Inspector integration allows attaching Chrome DevTools to Node.js
2301cb0ef41Sopenharmony_ciinstances for debugging and profiling. It uses the
2311cb0ef41Sopenharmony_ci[Chrome DevTools Protocol][].
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ciV8 Inspector can be enabled by passing the `--inspect` flag when starting a
2341cb0ef41Sopenharmony_ciNode.js application. It is also possible to supply a custom port with that flag,
2351cb0ef41Sopenharmony_cie.g. `--inspect=9222` will accept DevTools connections on port 9222.
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ciTo break on the first line of the application code, pass the `--inspect-brk`
2381cb0ef41Sopenharmony_ciflag instead of `--inspect`.
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci```console
2411cb0ef41Sopenharmony_ci$ node --inspect index.js
2421cb0ef41Sopenharmony_ciDebugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
2431cb0ef41Sopenharmony_ciFor help, see: https://nodejs.org/en/docs/inspector
2441cb0ef41Sopenharmony_ci```
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
2471cb0ef41Sopenharmony_ciat the end of the URL is generated on the fly, it varies in different
2481cb0ef41Sopenharmony_cidebugging sessions.)
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ciIf the Chrome browser is older than 66.0.3345.0,
2511cb0ef41Sopenharmony_ciuse `inspector.html` instead of `js_app.html` in the above URL.
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciChrome DevTools doesn't support debugging [worker threads][] yet.
2541cb0ef41Sopenharmony_ci[ndb][] can be used to debug them.
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
2571cb0ef41Sopenharmony_ci[`debugger`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger
2581cb0ef41Sopenharmony_ci[ndb]: https://github.com/GoogleChromeLabs/ndb/
2591cb0ef41Sopenharmony_ci[worker threads]: worker_threads.md
260