- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v18.20.8 文档
- Node.js v18.20.8
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
调试器#>
【Debugger】
Node.js 包含一个命令行调试工具。Node.js 调试器客户端并不是一个功能齐全的调试器,但可以进行简单的逐步执行和检查。
【Node.js includes a command-line debugging utility. The Node.js debugger client is not a full-featured debugger, but simple stepping and inspection are possible.】
要使用它,请使用 inspect 参数启动 Node.js,然后跟上要调试的脚本路径。
【To use it, start Node.js with the inspect argument followed by the path to the
script to debug.】
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
< For help, see: https://nodejs.cn/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
ok
Break on start in myscript.js:2
1 // myscript.js
> 2 global.x = 5;
3 setTimeout(() => {
4 debugger;
debug>
调试器会自动在第一个可执行行处中断。若希望运行到第一个断点(由 debugger 语句指定),请将环境变量 NODE_INSPECT_RESUME_ON_START 设置为 1。
【The debugger automatically breaks on the first executable line. To instead
run until the first breakpoint (specified by a debugger statement), set
the NODE_INSPECT_RESUME_ON_START environment variable to 1.】
$ cat myscript.js
// myscript.js
global.x = 5;
setTimeout(() => {
debugger;
console.log('world');
}, 1000);
console.log('hello');
$ NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/f1ed133e-7876-495b-83ae-c32c6fc319c2
< For help, see: https://nodejs.cn/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
< hello
<
break in myscript.js:4
2 global.x = 5;
3 setTimeout(() => {
> 4 debugger;
5 console.log('world');
6 }, 1000);
debug> next
break in myscript.js:5
3 setTimeout(() => {
4 debugger;
> 5 console.log('world');
6 }, 1000);
7 console.log('hello');
debug> repl
Press Ctrl+C to leave debug repl
> x
5
> 2 + 2
4
debug> next
< world
<
break in myscript.js:6
4 debugger;
5 console.log('world');
> 6 }, 1000);
7 console.log('hello');
8
debug> .exit
$
repl 命令允许远程评估代码。next 命令会执行到下一行。输入 help 查看其他可用命令。
【The repl command allows code to be evaluated remotely. The next command
steps to the next line. Type help to see what other commands are available.】
在未输入命令而按下 enter 时,将会重复上一条调试命令。
【Pressing enter without typing a command will repeat the previous debugger
command.】
监视器#>
【Watchers】
在调试时可以查看表达式和变量的值。在每个断点处,监视器列表中的每个表达式都会在当前上下文中进行求值,并在断点的源代码列表之前立即显示。
【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.】
要开始观察一个表达式,输入 watch('my_expression')。命令 watchers 会显示当前有效的观察者。要移除一个观察者,输入 unwatch('my_expression')。
【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').】
命令参考资料#>
【Command reference】
步进#>
【Stepping】
cont,c:继续执行next,n:执行下一步step,s:步骤out,o:走出去pause:暂停正在运行的代码(类似于开发者工具中的暂停按钮)
断点#>
【Breakpoints】
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第 1 行的断点
也可以在尚未加载的文件(模块)中设置断点:
【It is also possible to set a breakpoint in a file (module) that is not loaded yet:】
$ node inspect main.js
< Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
< For help, see: https://nodejs.cn/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
Break on start in main.js:1
> 1 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 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
>22 exports.hello = function() {
23 return 'hello from module';
24 };
debug>
也可以设置一个条件断点,仅当给定表达式计算结果为 true 时才会中断:
【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.cn/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< 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>
信息#>
【Information】
backtrace,bt:打印当前执行帧的回溯list(5):列出脚本源代码,并显示5行上下文(前后各5行)watch(expr): 将表达式添加到监视列表unwatch(expr):从监视列表中移除表达式unwatch(index):从监视列表中移除指定索引的表达式watchers:列出所有监视器及其值(在每个断点自动列出)repl:在调试脚本的上下文中打开调试器的 REPL 以进行评估exec expr,p expr:在调试脚本的上下文中执行一个表达式并打印其值profile:开始 CPU 分析会话profileEnd:停止当前的 CPU 分析会话profiles:列出所有已完成的 CPU 分析会话profiles[n].save(filepath = 'node.cpuprofile'):将 CPU 性能分析会话保存为 JSON 到磁盘takeHeapSnapshot(filepath = 'node.heapsnapshot'):拍摄堆快照并将其作为 JSON 保存到磁盘
执行控制#>
【Execution control】
run:运行脚本(在调试器启动时自动运行)restart:重启脚本kill:终止脚本
其他#>
【Various】
scripts:列出所有已加载的脚本version: 显示 V8 的版本
高级用法#>
【Advanced usage】
Node.js 的 V8 检查器集成#>
【V8 inspector integration for Node.js】
V8 Inspector 集成允许将 Chrome DevTools 附加到 Node.js 实例以进行调试和分析。它使用 Chrome 开发者工具协议。
【V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling. It uses the Chrome DevTools Protocol.】
可以通过在启动 Node.js 应用时传递 --inspect 标志来启用 V8 Inspector。也可以通过该标志指定自定义端口,例如 --inspect=9222 将在端口 9222 上接受 DevTools 连接。
【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.】
要在应用代码的第一行中断,请传递 --inspect-brk 标志,而不是 --inspect。
【To break on the first line of the application code, pass the --inspect-brk
flag instead of --inspect.】
$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
For help, see: https://nodejs.cn/docs/inspector
(在上面的示例中,URL 末尾的 UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 是动态生成的,在不同的调试会话中会有所不同。)
如果 Chrome 浏览器版本低于 66.0.3345.0,请在上述 URL 中使用 inspector.html 而不是 js_app.html。
【If the Chrome browser is older than 66.0.3345.0,
use inspector.html instead of js_app.html in the above URL.】
Chrome 开发者工具尚不支持调试 工作线程。可以使用 ndb 来调试它们。
【Chrome DevTools doesn't support debugging worker threads yet. ndb can be used to debug them.】