2012-02-27 20:08:17 +01:00
|
|
|
# Debugger
|
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.9.12-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-02-27 20:44:30 +01:00
|
|
|
<!-- type=misc -->
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2016-11-28 21:38:23 +01:00
|
|
|
Node.js includes an out-of-process debugging utility accessible via a
|
2017-09-09 13:09:28 +02:00
|
|
|
[V8 Inspector][] and built-in debugging client. To use it, start Node.js
|
2018-02-12 08:31:55 +01:00
|
|
|
with the `inspect` argument followed by the path to the script to debug; a
|
|
|
|
prompt will be displayed indicating successful launch of the debugger:
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
```console
|
2017-06-23 04:17:11 +02:00
|
|
|
$ node inspect myscript.js
|
|
|
|
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
|
2018-04-07 15:57:52 +02:00
|
|
|
< For help, see: https://nodejs.org/en/docs/inspector
|
2017-06-23 04:17:11 +02:00
|
|
|
< Debugger attached.
|
|
|
|
Break on start in myscript.js:1
|
|
|
|
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
|
2016-01-24 10:15:51 +01:00
|
|
|
2 setTimeout(() => {
|
2017-06-23 04:17:11 +02:00
|
|
|
3 console.log('world');
|
2016-01-17 18:39:07 +01:00
|
|
|
debug>
|
|
|
|
```
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2016-05-27 01:35:23 +02:00
|
|
|
Node.js's debugger client is not a full-featured debugger, but simple step and
|
|
|
|
inspection are possible.
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2015-12-27 06:19:35 +01:00
|
|
|
Inserting the statement `debugger;` into the source code of a script will
|
2016-05-27 01:35:23 +02:00
|
|
|
enable a breakpoint at that position in the code:
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2017-04-21 21:55:51 +02:00
|
|
|
<!-- eslint-disable no-debugger -->
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// myscript.js
|
2017-01-21 21:38:18 +01:00
|
|
|
global.x = 5;
|
2016-01-24 10:15:51 +01:00
|
|
|
setTimeout(() => {
|
2016-01-17 18:39:07 +01:00
|
|
|
debugger;
|
|
|
|
console.log('world');
|
|
|
|
}, 1000);
|
|
|
|
console.log('hello');
|
|
|
|
```
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2017-01-21 21:38:18 +01:00
|
|
|
Once the debugger is run, a breakpoint will occur at line 3:
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
```console
|
2017-06-23 04:17:11 +02:00
|
|
|
$ node inspect myscript.js
|
|
|
|
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
|
2018-04-07 15:57:52 +02:00
|
|
|
< For help, see: https://nodejs.org/en/docs/inspector
|
2017-06-23 04:17:11 +02:00
|
|
|
< Debugger attached.
|
|
|
|
Break on start in myscript.js:1
|
|
|
|
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
|
2016-01-24 10:15:51 +01:00
|
|
|
2 setTimeout(() => {
|
2016-01-17 18:39:07 +01:00
|
|
|
3 debugger;
|
|
|
|
debug> cont
|
|
|
|
< hello
|
2017-06-23 04:17:11 +02:00
|
|
|
break in myscript.js:3
|
|
|
|
1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
|
2016-01-24 10:15:51 +01:00
|
|
|
2 setTimeout(() => {
|
2017-01-21 21:38:18 +01:00
|
|
|
> 3 debugger;
|
2016-01-17 18:39:07 +01:00
|
|
|
4 console.log('world');
|
|
|
|
5 }, 1000);
|
|
|
|
debug> next
|
2017-06-23 04:17:11 +02:00
|
|
|
break in myscript.js:4
|
2016-01-24 10:15:51 +01:00
|
|
|
2 setTimeout(() => {
|
2016-01-17 18:39:07 +01:00
|
|
|
3 debugger;
|
2017-01-21 21:38:18 +01:00
|
|
|
> 4 console.log('world');
|
2016-01-17 18:39:07 +01:00
|
|
|
5 }, 1000);
|
|
|
|
6 console.log('hello');
|
|
|
|
debug> repl
|
|
|
|
Press Ctrl + C to leave debug repl
|
|
|
|
> x
|
|
|
|
5
|
2018-04-02 07:38:48 +02:00
|
|
|
> 2 + 2
|
2016-01-17 18:39:07 +01:00
|
|
|
4
|
|
|
|
debug> next
|
2017-01-21 21:38:18 +01:00
|
|
|
< world
|
2017-06-23 04:17:11 +02:00
|
|
|
break in myscript.js:5
|
2016-01-17 18:39:07 +01:00
|
|
|
3 debugger;
|
|
|
|
4 console.log('world');
|
2017-01-21 21:38:18 +01:00
|
|
|
> 5 }, 1000);
|
2016-01-17 18:39:07 +01:00
|
|
|
6 console.log('hello');
|
|
|
|
7
|
2017-06-23 04:17:11 +02:00
|
|
|
debug> .exit
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2015-12-27 06:19:35 +01:00
|
|
|
The `repl` command allows code to be evaluated remotely. The `next` command
|
2016-05-27 01:35:23 +02:00
|
|
|
steps to the next line. Type `help` to see what other commands are available.
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2016-04-06 02:17:33 +02:00
|
|
|
Pressing `enter` without typing a command will repeat the previous debugger
|
|
|
|
command.
|
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
## Watchers
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2015-12-27 06:19:35 +01:00
|
|
|
It is possible to watch expression and variable values while debugging. On
|
|
|
|
every breakpoint, each expression from the watchers list will be evaluated
|
|
|
|
in the current context and displayed immediately before the breakpoint's
|
|
|
|
source code listing.
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2015-12-27 06:19:35 +01:00
|
|
|
To begin watching an expression, type `watch('my_expression')`. The command
|
|
|
|
`watchers` will print the active watchers. To remove a watcher, type
|
|
|
|
`unwatch('my_expression')`.
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2016-05-27 01:35:23 +02:00
|
|
|
## Command reference
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
### Stepping
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
* `cont`, `c`: Continue execution
|
|
|
|
* `next`, `n`: Step next
|
|
|
|
* `step`, `s`: Step in
|
|
|
|
* `out`, `o`: Step out
|
|
|
|
* `pause`: Pause running code (like pause button in Developer Tools)
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
### Breakpoints
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
* `setBreakpoint()`, `sb()`: Set breakpoint on current line
|
|
|
|
* `setBreakpoint(line)`, `sb(line)`: Set breakpoint on specific line
|
|
|
|
* `setBreakpoint('fn()')`, `sb(...)`: Set breakpoint on a first statement in
|
2011-10-01 09:28:57 +02:00
|
|
|
functions body
|
2019-10-24 06:28:42 +02:00
|
|
|
* `setBreakpoint('script.js', 1)`, `sb(...)`: Set breakpoint on first line of
|
2018-04-29 19:46:41 +02:00
|
|
|
`script.js`
|
2019-10-24 06:28:42 +02:00
|
|
|
* `clearBreakpoint('script.js', 1)`, `cb(...)`: Clear breakpoint in `script.js`
|
2014-12-16 17:27:31 +01:00
|
|
|
on line 1
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2013-04-26 08:05:33 +02:00
|
|
|
It is also possible to set a breakpoint in a file (module) that
|
2017-01-24 20:37:46 +01:00
|
|
|
is not loaded yet:
|
2013-04-26 08:05:33 +02:00
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
```console
|
2017-09-01 23:42:27 +02:00
|
|
|
$ node inspect main.js
|
2017-06-23 04:17:11 +02:00
|
|
|
< Debugger listening on ws://127.0.0.1:9229/4e3db158-9791-4274-8909-914f7facf3bd
|
2018-04-07 15:57:52 +02:00
|
|
|
< For help, see: https://nodejs.org/en/docs/inspector
|
2017-06-23 04:17:11 +02:00
|
|
|
< Debugger attached.
|
2017-09-01 23:42:27 +02:00
|
|
|
Break on start in main.js:1
|
2017-06-23 04:17:11 +02:00
|
|
|
> 1 (function (exports, require, module, __filename, __dirname) { const mod = require('./mod.js');
|
2016-01-17 18:39:07 +01:00
|
|
|
2 mod.hello();
|
|
|
|
3 mod.hello();
|
2017-06-23 04:17:11 +02:00
|
|
|
debug> setBreakpoint('mod.js', 22)
|
2016-01-17 18:39:07 +01:00
|
|
|
Warning: script 'mod.js' was not loaded yet.
|
|
|
|
debug> c
|
2017-09-01 23:42:27 +02:00
|
|
|
break in mod.js:22
|
2017-06-23 04:17:11 +02:00
|
|
|
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
21
|
|
|
|
>22 exports.hello = function() {
|
|
|
|
23 return 'hello from module';
|
|
|
|
24 };
|
2016-01-17 18:39:07 +01:00
|
|
|
debug>
|
|
|
|
```
|
2013-04-26 08:05:33 +02:00
|
|
|
|
2016-05-27 01:35:23 +02:00
|
|
|
### Information
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
* `backtrace`, `bt`: Print backtrace of current execution frame
|
|
|
|
* `list(5)`: List scripts source code with 5 line context (5 lines before and
|
2011-10-01 09:28:57 +02:00
|
|
|
after)
|
2019-10-24 06:28:42 +02:00
|
|
|
* `watch(expr)`: Add expression to watch list
|
|
|
|
* `unwatch(expr)`: Remove expression from watch list
|
|
|
|
* `watchers`: List all watchers and their values (automatically listed on each
|
2011-10-01 09:28:57 +02:00
|
|
|
breakpoint)
|
2019-10-24 06:28:42 +02:00
|
|
|
* `repl`: Open debugger's repl for evaluation in debugging script's context
|
|
|
|
* `exec expr`: Execute an expression in debugging script's context
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
### Execution control
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
* `run`: Run script (automatically runs on debugger's start)
|
|
|
|
* `restart`: Restart script
|
|
|
|
* `kill`: Kill script
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
### Various
|
2011-10-01 09:28:57 +02:00
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
* `scripts`: List all loaded scripts
|
|
|
|
* `version`: Display V8's version
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2012-02-27 20:08:17 +01:00
|
|
|
## Advanced Usage
|
2010-12-31 03:46:49 +01:00
|
|
|
|
2016-12-17 19:51:15 +01:00
|
|
|
### V8 Inspector Integration for Node.js
|
2016-02-07 17:47:14 +01:00
|
|
|
|
|
|
|
V8 Inspector integration allows attaching Chrome DevTools to Node.js
|
2018-02-12 08:31:55 +01:00
|
|
|
instances for debugging and profiling. It uses the
|
2018-04-17 19:04:03 +02:00
|
|
|
[Chrome DevTools Protocol][].
|
2016-02-07 17:47:14 +01:00
|
|
|
|
|
|
|
V8 Inspector can be enabled by passing the `--inspect` flag when starting a
|
|
|
|
Node.js application. It is also possible to supply a custom port with that flag,
|
|
|
|
e.g. `--inspect=9222` will accept DevTools connections on port 9222.
|
|
|
|
|
2017-02-22 08:33:57 +01:00
|
|
|
To break on the first line of the application code, pass the `--inspect-brk`
|
|
|
|
flag instead of `--inspect`.
|
2016-02-07 17:47:14 +01:00
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
```console
|
2016-09-29 10:43:51 +02:00
|
|
|
$ node --inspect index.js
|
2017-04-14 03:01:08 +02:00
|
|
|
Debugger listening on 127.0.0.1:9229.
|
2016-09-29 10:43:51 +02:00
|
|
|
To start debugging, open the following URL in Chrome:
|
2018-06-18 14:14:19 +02:00
|
|
|
chrome-devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
|
2016-09-29 10:43:51 +02:00
|
|
|
```
|
|
|
|
|
2017-01-21 21:38:18 +01:00
|
|
|
(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
|
|
|
|
at the end of the URL is generated on the fly, it varies in different
|
|
|
|
debugging sessions.)
|
|
|
|
|
2018-06-18 14:14:19 +02:00
|
|
|
If the Chrome browser is older than 66.0.3345.0,
|
|
|
|
use `inspector.html` instead of `js_app.html` in the above URL.
|
|
|
|
|
2019-11-22 18:29:58 +01:00
|
|
|
Chrome DevTools doesn't support debugging [Worker Threads][] yet.
|
|
|
|
[ndb][] can be used to debug them.
|
|
|
|
|
2018-04-17 19:04:03 +02:00
|
|
|
[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
|
2017-09-09 13:09:28 +02:00
|
|
|
[V8 Inspector]: #debugger_v8_inspector_integration_for_node_js
|
2019-11-22 18:29:58 +01:00
|
|
|
[Worker Threads]: worker_threads.html
|
|
|
|
[ndb]: https://github.com/GoogleChromeLabs/ndb/
|