Nodejs入门指南(2)

这篇指南介绍了如何启用Node.js的Inspector进行调试,包括使用--inspect选项、Inspector客户端如node-inspect、Chrome DevTools、VS Code和JetBrains WebStorm。同时提到了传统调试器和命令行选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻译自:https://nodejs.org/en/docs/guides/debugging-getting-started/

调试指南 (Debugging Guide)

本指南将帮助您开始调试Node.js应用程序和脚本

启用检查器 (Enable Inspector)
NOTE:--inspect选项和Inspector协议是实验性的,可能会更改
    当使用--inspect开关启动时,Node.js进程通过WebSockets侦听检查器协议定义的诊断命令,默认情况下在主机和端口127.0.0.1:9229。每个进程也被分配一个唯一的UUID(例如0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e)。
检测器客户端必须知道并指定主机地址,端口和UUID才能连接到WebSocket接口。完整的URL是ws://127.0.0.1:9229 / 0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e,当然取决于实际的主机和端口以及实例的正确UUID。
Inspector还包含一个HTTP端点,用于提供有关调试对象的元数据,包括其WebSocket URL,UUID和Chrome DevTools URL。通过向http:// [host:port] / json / list发送HTTP请求来获取此元数据。这会返回如下的JSON对象;使用webSocketDebuggerUrl属性作为URL直接连接到Inspector。
{
  "description": "node.js instance",
  "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e",
  "faviconUrl": "https://nodejs.org/static/favicon.ico",
  "id": "0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e",
  "title": "node",
  "type": "node",
  "url": "file://",
  "webSocketDebuggerUrl": "ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e"
}

在没有--inspect的情况下启动的Node.js进程也可以被委派通过用SIGUSR1(在Linux和OS X上)发信号来开始侦听调试消息。从Node7开始,这将激活传统的调试器API;在Node8和更高版本中,它将激活Inspector API。

客户端检查器 (Inspector Clients)
一些商业和开源工具可以集成Node的检查器,一般有如下:
node-inspect

  • 用 Inspector Protocol协议的Nodejs组织提供的CLI工具。
  • 跟Node绑定的,可以这样使用 node inspect myscript.js.
  • 也可以单独安装最新版(e.g. npm install -g node-inspect

Chrome DevTools 55+
VS Code 1.10+
  • In the Debug panel, click the settings icon to open .vscode/launch.json. Select "Node.js" for initial setup.
JetBrains WebStorm 2017.1+ and other JetBrains IDEs
  • Create a new Node.js debug configuration and hit Debug. --inspect will be used by default for Node.js 7+. To disable uncheck js.debugger.node.use.inspect in the IDE Registry.
chrome-remote-interface
  • Library to ease connections to Inspector Protocol endpoints.

命令行选项(Command-line options)

标示含义
--inspect
  • 启用检查器代理
  • 监听默认地址和端口 (127.0.0.1:9229)
--inspect=[host:port]
  • 启用检查器代理
  • 绑定地址或主机名 (default: 127.0.0.1)
  • 监听端口 (default: 9229)
--inspect-brk
  • 启用检查器代理
  • 监听默认地址和端口 (127.0.0.1:9229)
  • Break before user code starts(在用户代码开始之前中断)
--inspect-brk=[host:port]
  • 启动检查器代理
  • 绑定地址或主机名 (default: 127.0.0.1)
  • 监听默认地址和端口 (default: 9229)
  • Break before user code starts(在用户代码开始之前中断)
node inspectscript.js
  • Spawn child process to run user's script under --inspect flag; and use main process to run CLI debugger.(用--inspect生成子进程运行用户脚本,并使用主进程运行CLI调试器)

传统的调试器(Legacy Debugger)

旧版调试器自7.7.0版开始已弃用。请使用--inspect和Inspector。

When started with the --debug or --debug-brk switches in version 7 and earlier, Node.js listens for debugging commands defined by the discontinued V8 Debugging Protocol on a TCP port, by default 5858. Any debugger client which speaks this protocol can connect to and debug the running process; a couple popular ones are listed below.

V8调试协议不再维护或记录
Built-in Debugger
Start  node debug script_name.js  to start your script under Node's builtin command-line debugger. Your script starts in another Node process started with the  --debug-brk  option, and the initial Node process runs the  _debugger.js  script and connects to your target.

node-inspector
Debug your Node.js app with Chrome DevTools by using an intermediary process which translates the Inspector Protocol used in Chromium to the V8 Debugger protocol used in Node.js.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值