翻译自: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+
- Option 1: Open
chrome://inspect
in a Chromium-based browser. Click the Configure button and ensure your target host and port are listed. - Option 2: Copy the
devtoolsFrontendUrl
from the output of/json/list
(see above) or the --inspect hint text and paste into Chrome. - Option 3: Install the Chrome Extension NIM (Node Inspector Manager):https://chrome.google.com/webstore/detail/nim-node-inspector-manage/gnhhdgbaldcilmgcpfddgdbkhjohddkj
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 uncheckjs.debugger.node.use.inspect
in the IDE Registry.
chrome-remote-interface
- Library to ease connections to Inspector Protocol endpoints.
命令行选项(Command-line options)
标示 | 含义 |
---|---|
--inspect |
|
--inspect=[host:port] |
|
--inspect-brk |
|
--inspect-brk=[host:port] |
|
node inspectscript.js |
|
传统的调试器(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.
Built-in Debugger
Startnode 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.来源:CSDN
作者:G-god
链接:https://blog.csdn.net/wknhsj1/article/details/79506868