debugger(调试器)
Node.js 包含了一个进程外的调试实用程序,可通过 V8 检查器或内置的调试客户端访问。
要使用它,请使用 inspect 参数启动 Node.js,并带上要调试的脚本的路径。
如果出现提示符,则表明调试器已成功启动:
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
2 setTimeout(() => {
3 console.log('world');
debug>
Node.js 的调试器客户端不是一个具有全部特性的调试器,但可以进行简单的单步执行和调试。
把 debugger; 语句插入到脚本的源代码,则将会在代码中的该位置启用一个断点:
// myscript.js
global.x = 5;
setTimeout(() => {
debugger;
console.log('世界');
}, 1000);
console.log('你好');
一旦执行调试器,则将会在第 3 行出现一个断点:
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
2 setTimeout(() => {
3 debugger;
debug> cont
< 你好
break in myscript.js:3
1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
2 setTimeout(() => {
> 3 debugger;
4 console.log('世界');
5 }, 1000);
debug> next
break in myscript.js:4
2 setTimeout(() => {
3 debugger;
> 4 console.log('世界');
5 }, 1000);
6 console.log('你好');
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2 + 2
4
debug> next
< 世界
break in myscript.js:5
3 debugger;
4 console.log('世界');
> 5 }, 1000);
6 console.log('你好');
7
debug> .exit
repl 命令允许远程地运行代码。
next 命令会单步进入下一行。
键入 help 可以查看其他可用的命令。
在不键入命令的情况下按 enter 键,则会重复上一个调试器命令。
监视器
当调试时,可以监视表达式和变量的值。
在每个断点处,监视器列表中的每个表达式将会在当前上下文中被执行,并立刻显示在断点的源码列表之前。
要开始监视表达式,则键入 watch('my_expression')。
watchers 命令将会打印现役的监视器。
要移除监视器,则键入 unwatch('my_expression')。
命令参考手册
单步执行cont, c: 继续执行。
next, n: 单步执行下一行。
step, s: 单步进入。
out, o: 单步退出。
pause: 暂停运行中的代码(类似于开发者工具中的暂停按钮)。
断点setBreakpoint(), sb(): 在当前行上设置断点。
setBreakpoint(line), sb(line): 在指定行上设置断点。
setBreakpoint('fn()'), sb(...): 在函数体的第一个语句上设置断点。
setBreakpoint('script.js', 1)、 sb(...): 在 script.js 的第一行上设置断点。
setBreakpoint('script.js', 1, 'num < 4')、 sb(...): 在 script.js 的第一行上设置条件断点,仅当 num < 4 计算为 true 时才会中断。
clearBreakpoint('script.js', 1), cb(...): 清除 script.js 中第一行上的断点。
也可以在尚未加载的文件(模块)中设置断点:
$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/4e3db158-9791-4274-8909-914f7facf3bd
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in main.js:1
> 1 (function (exports, require, module, __filename, __dirname) { const mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> setBreakpoint('mod.js', 22)
Warning: script 'mod.js' was not loaded yet.
debug> c
break in mod.js:22
20 // 软件中的其他处理。
21
>22 exports.hello = function() {
23 return '来自模块的问候';
24 };
debug>
It is also possible to set a conditional breakpoint that only breaks when a
given expression evaluates to true:
$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in main.js:7
5 }
6
> 7 addOne(10);
8 addOne(-1);
9
debug> setBreakpoint('main.js', 4, 'num < 0')
1 'use strict';
2
3 function addOne(num) {
> 4 return num + 1;
5 }
6
7 addOne(10);
8 addOne(-1);
9
debug> cont
break in main.js:4
2
3 function addOne(num) {
> 4 return num + 1;
5 }
6
debug> exec('num')
-1
debug>
信息backtrace, bt: 打印当前执行帧的回溯。
list(5): 列出脚本源码的 5 行上下文(前后各 5 行)。
watch(expr): 将表达式添加到监视列表。
unwatch(expr): 从监视列表中移除表达式。
watchers: 列出所有的监视器和它们的值(在每个断点上自动地列出)。
repl: 打开调试器的 repl,用于调试脚本的上下文中的执行。
exec expr: 在调试脚本的上下文中执行一个表达式。
执行的控制run: 运行脚本(在调试器启动时自动地运行)。
restart: 重启脚本。
kill: 杀死脚本。
杂项scripts: 列出所有已加载的脚本。
version: 显示 V8 的版本。
高级用法
用于Node.js的V8调试器的集成
V8 检查器的集成允许将 Chrome 开发者工具附加到 Node.js 实例,以便进行调试和性能分析。
它使用了 Chrome 开发者工具协议。
可以通过在启动 Node.js 应用程序时传入 --inspect 标志来启用 V8 检查器。
也可以使用该标志提供自定义的端口,例如 --inspect=9222 将会在 9222 端口上接受开发者工具的连接。
如果要在应用程序代码的第一行进行断点,则传入 --inspect-brk 标志而不是 --inspect。
$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
For help, see: https://nodejs.org/en/docs/inspector
(在上面的示例中,URL 末尾的 UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 是动态生成的,它在不同的调试会话中是不一样的。)
如果 Chrome 浏览器的版本低于 66.0.3345.0,则在上述的 URL 中使用 inspector.html 而不是 js_app.html。
Chrome 开发者工具目前还不支持调试工作线程。
可以使用 ndb 来调试它们。