Node.js 的 V8 检查器集成
¥V8 inspector integration for Node.js
V8 检查器集成允许将 Chrome 开发者工具绑定到 Node.js 实例以进行调试和分析。它使用 Chrome DevTools 协议。
¥V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling. It uses the Chrome DevTools Protocol.
V8 检查器可以通过在启动 Node.js 应用时传入 --inspect
标志来启用。也可以使用该标志提供自定义的端口,例如 --inspect=9222
将接受端口 9222 上的开发者工具连接。
¥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
标志将在连接调试器之前立即执行代码。这意味着代码将在你开始调试之前开始运行,如果你想从一开始就进行调试,这可能并不理想。
¥Using the --inspect
flag will execute the code immediately before debugger is connected.
This means that the code will start running before you can start debugging, which might
not be ideal if you want to debug from the very beginning.
在这种情况下,你有两种选择:
¥In such cases, you have two alternatives:
-
--inspect-wait
标志:该标志将在执行代码之前等待附加调试器。这允许你从执行一开始就开始调试。¥
--inspect-wait
flag: This flag will wait for debugger to be attached before executing the code. This allows you to start debugging right from the beginning of the execution. -
--inspect-brk
标志:与--inspect
不同,一旦附加调试器,该标志就会在代码的第一行中断。当你想要从一开始就逐步调试代码而不在调试之前执行任何代码时,这非常有用。¥
--inspect-brk
flag: Unlike--inspect
, this flag will break on the first line of the code as soon as debugger is attached. This is useful when you want to debug the code step by step from the very beginning, without any code execution prior to debugging.
因此,在选择 --inspect
、--inspect-wait
和 --inspect-brk
时,请考虑是否希望代码立即开始执行、等待调试器连接后再执行,还是在第一行中断以进行逐步调试。
¥So, when deciding between --inspect
, --inspect-wait
, and --inspect-brk
, consider whether you want
the code to start executing immediately, wait for debugger to be attached before execution,
or break on the first line for step-by-step debugging.
$ 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
(在上面的示例中,网址末尾的 UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 是动态生成的,它在不同的调试会话中有所不同。)
¥(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.)
如果 Chrome 浏览器版本低于 66.0.3345.0,则在上述网址中使用 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 DevTools 尚不支持调试 工作线程。ndb 可用于调试它们。
¥Chrome DevTools doesn't support debugging worker threads yet. ndb can be used to debug them.