{/* 此页面由 website/scripts/generate-skill-docs.py 从技能的 SKILL.md 自动生成。请编辑源文件 SKILL.md,而非此页面。 */}
Node Inspect 调试器
通过 —inspect + Chrome DevTools 协议 CLI 调试 Node.js。
技能元数据
| 来源 | 内置(默认已安装) |
| 路径 | skills/software-development/node-inspect-debugger |
| 版本 | 1.0.0 |
| 作者 | Hermes Agent |
| 许可证 | MIT |
| 平台 | linux, macos, windows |
| 标签 | debugging、nodejs、node-inspect、cdp、breakpoints、ui-tui |
| 相关技能 | systematic-debugging、python-debugpy、debugging-hermes-tui-commands |
参考:完整 SKILL.md
:::info 以下是当此技能被触发时 Hermes 加载的完整技能定义。这是技能激活时代理所看到的指令。 :::
Node.js Inspect 调试器
概述
当 console.log 不够用时,从终端以编程方式驱动 Node 内置的 V8 检查器。你可以获得真正的断点、单步执行、调用栈追踪、局部/闭包作用域转储以及在暂停帧中执行任意表达式求值。
两种工具,任选其一:
node inspect— 内置,零安装,CLI REPL。最适合快速探查。ndb/ 通过chrome-remote-interface的 CDP — 可从 Node/Python 脚本化;最适合自动设置多个断点、跨运行收集状态,或在代理循环中以非交互方式进行调试。
优先选择 node inspect。 它始终可用且 REPL 响应迅速。
何时使用
- Node 测试失败,需要查看中间状态
- ui-tui 崩溃或行为异常,希望在预渲染时检查 React/Ink 状态
- tui_gateway 子进程(
_SlashWorker、PTY 桥接工作器)行为异常 - 需要检查闭包中的某个值,且
console.log在无需修补代码的情况下无法访问 - 性能分析:附加到正在运行的进程以捕获 CPU 性能分析或堆快照
不要用于: console.log 在一分钟内就能解决的问题。断点驱动的调试更重,只在有实际收益时使用。
快速参考:node inspect REPL
在第一行暂停启动:
node inspect path/to/script.js
# 或使用 tsx
node --inspect-brk $(which tsx) path/to/script.tsdebug> 提示符接受以下命令:
| 命令 | 操作 |
|---|---|
c 或 cont | 继续执行 |
n 或 next | 单步跳过 |
s 或 step | 单步进入 |
o 或 out | 单步跳出 |
pause | 暂停正在运行的代码 |
sb('file.js', 42) | 在 file.js 的第 42 行设置断点 |
sb(42) | 在当前文件的第 42 行设置断点 |
sb('functionName') | 在函数被调用时中断 |
cb('file.js', 42) | 清除断点 |
breakpoints | 列出所有断点 |
bt | 回溯(调用栈) |
list(5) | 显示当前位置周围 5 行源码 |
watch('expr') | 每次暂停时求值表达式 |
watchers | 显示被监视的表达式 |
repl | 进入当前作用域的 REPL(Ctrl+C 退出 REPL) |
exec expr | 一次性求值表达式 |
restart | 重启脚本 |
kill | 终止脚本 |
.exit | 退出调试器 |
在 repl 子模式中: 输入任意 JS 表达式,包括访问局部/闭包变量。Ctrl+C 返回 debug>。
附加到正在运行的进程
当进程已在运行中(例如长期运行的开发服务器或 TUI Gateway):
# 1. 向已有进程发送 SIGUSR1 以启用检查器
kill -SIGUSR1 <pid>
# Node 会打印:Debugger listening on ws://127.0.0.1:9229/<uuid>
# 2. 附加调试器 CLI
node inspect -p <pid>
# 或通过 URL
node inspect ws://127.0.0.1:9229/<uuid>从开始就启动带有检查器的进程:
node --inspect script.js # 监听 127.0.0.1:9229,继续运行
node --inspect-brk script.js # 监听并在第一行暂停
node --inspect=0.0.0.0:9230 script.js # 自定义主机:端口通过 tsx 运行 TypeScript:
node --inspect-brk --import tsx script.ts
# 或旧版 tsx
node --inspect-brk -r tsx/cjs script.ts编程式 CDP(从终端脚本化)
当需要自动化操作时——设置多个断点、捕获作用域状态、编写重现脚本——使用 chrome-remote-interface:
npm i -g chrome-remote-interface # 或项目本地
# 启动目标:
node --inspect-brk=9229 target.js &驱动脚本(保存为 /tmp/cdp-debug.js):
const CDP = require('chrome-remote-interface');
(async () => {
const client = await CDP({ port: 9229 });
const { Debugger, Runtime } = client;
Debugger.paused(async ({ callFrames, reason }) => {
const top = callFrames[0];
console.log(`PAUSED: ${reason} @ ${top.url}:${top.location.lineNumber + 1}`);
// 遍历作用域获取局部变量
for (const scope of top.scopeChain) {
if (scope.type === 'local' || scope.type === 'closure') {
const { result } = await Runtime.getProperties({
objectId: scope.object.objectId,
ownProperties: true,
});
for (const p of result) {
console.log(` ${scope.type}.${p.name} =`, p.value?.value ?? p.value?.description);
}
}
}
// 在暂停帧中求值表达式
const { result } = await Debugger.evaluateOnCallFrame({
callFrameId: top.callFrameId,
expression: 'typeof state !== "undefined" ? JSON.stringify(state) : "n/a"',
});
console.log('state =', result.value ?? result.description);
await Debugger.resume();
});
await Runtime.enable();
await Debugger.enable();
// 通过 URL 正则 + 行号设置断点
await Debugger.setBreakpointByUrl({
urlRegex: '.*app\\.tsx$',
lineNumber: 119, // 0-indexed
columnNumber: 0,
});
await Runtime.runIfWaitingForDebugger();
})();运行:
node /tmp/cdp-debug.jsHermes 特定说明:chrome-remote-interface 不在 ui-tui/package.json 中。如果你不想污染项目,安装到临时位置:
mkdir -p /tmp/cdp-tools && cd /tmp/cdp-tools && npm i chrome-remote-interface
NODE_PATH=/tmp/cdp-tools/node_modules node /tmp/cdp-debug.js调试 Hermes ui-tui
TUI 基于 Ink + tsx 构建。两种常见场景:
在开发模式下调试单个 Ink 组件
ui-tui/package.json 中有 npm run dev(tsx —watch)。通过直接运行 tsx 添加 --inspect-brk:
cd /home/bb/hermes-agent/ui-tui
npm run build # 先生成 dist/,以便首次加载时无需转译
node --inspect-brk dist/entry.js
# 在另一个终端中:
node inspect -p <node pid>然后在 debug> 中:
sb('dist/app.js', 220) # 或可疑渲染位置
cont
暂停时,repl → 检查 props、state refs、useInput 处理值等。
调试正在运行的 hermes --tui
TUI 从 Python CLI 启动 Node。最简单的方法:
# 1. 启动 TUI
hermes --tui &
TUI_PID=$(pgrep -f 'ui-tui/dist/entry' | head -1)
# 2. 在该 Node PID 上启用检查器
kill -SIGUSR1 "$TUI_PID"
# 3. 查找 WS URL
curl -s http://127.0.0.1:9229/json/list | jq -r '.[0].webSocketDebuggerUrl'
# 4. 附加
node inspect ws://127.0.0.1:9229/<uuid>与 TUI 交互(在其窗口中输入)将继续推进执行;你的调试器可以在任何 sb(...) 断点处暂停。
调试 _SlashWorker / PTY 子进程
这些是 Python 进程,而非 Node——请使用 python-debugpy 技能。只有 Node 部分(Ink UI、tui_gateway 客户端、ui-tui/ 下 tsx 运行的测试)使用此技能。
在调试器下运行 Vitest 测试
cd /home/bb/hermes-agent/ui-tui
# 在入口处暂停运行单个测试文件
node --inspect-brk ./node_modules/vitest/vitest.mjs run --no-file-parallelism src/app/foo.test.tsx在另一个终端中:node inspect -p <pid>,然后 sb('src/app/foo.tsx', 42),cont。
使用 --no-file-parallelism(vitest)或 --runInBand(jest),以便只有一个工作器存在——调试池进程很麻烦。
堆快照和 CPU 性能分析(非交互式)
从上面的 CDP 驱动脚本中,将 Debugger 替换为 HeapProfiler / Profiler:
// 5 秒 CPU 性能分析
await client.Profiler.enable();
await client.Profiler.start();
await new Promise(r => setTimeout(r, 5000));
const { profile } = await client.Profiler.stop();
require('fs').writeFileSync('/tmp/cpu.cpuprofile', JSON.stringify(profile));
// 在 Chrome DevTools → Performance 选项卡中打开 /tmp/cpu.cpuprofile// 堆快照
await client.HeapProfiler.enable();
const chunks = [];
client.HeapProfiler.addHeapSnapshotChunk(({ chunk }) => chunks.push(chunk));
await client.HeapProfiler.takeHeapSnapshot({ reportProgress: false });
require('fs').writeFileSync('/tmp/heap.heapsnapshot', chunks.join(''));常见陷阱
-
TS 源码中的行号错误。 断点命中编译后的 JS,而非
.ts。要么 (a) 在构建后的dist/*.js中设断点,要么 (b) 启用 sourcemaps(node --enable-source-maps)并使用sb('src/app.tsx', N)——但仅限支持 sourcemaps 的 CDP 客户端。node inspectCLI 不支持。 -
--inspect与--inspect-brk。--inspect启动检查器但不暂停;如果你附加太晚,脚本会在第一个断点设置前执行完毕。需要在任何代码运行前设置断点时使用--inspect-brk。 -
端口冲突。 默认端口为
9229。如果多个 Node 进程正在检查,使用--inspect=0(随机端口)并从/json/list读取实际 URL:curl -s http://127.0.0.1:9229/json/list # 列出主机上所有可检查目标 -
子进程。 父进程上的
--inspect不会检查其子进程。使用NODE_OPTIONS='--inspect-brk' node parent.js传播给每个子进程;请注意它们都需要唯一端口(当继承NODE_OPTIONS='--inspect'时,Node 会自动递增)。 -
后台终止。 如果在目标暂停时用
Ctrl+C退出node inspect,目标会保持暂停状态。要么先cont,要么显式kill目标。 -
通过代理终端运行
node inspect。 这是一个 PTY 友好的 REPL。在 Hermes 中,使用terminal(pty=true)或background=true+process(action='submit', data='...')启动。非 PTY 前台模式适用于一次性命令,但不适用于交互式单步执行。 -
安全性。
--inspect=0.0.0.0:9229暴露任意代码执行。始终绑定到127.0.0.1(默认值),除非你处于隔离网络中。
验证清单
设置调试会话后,验证:
-
curl -s http://127.0.0.1:9229/json/list返回的正是你期望的目标 - 第一个断点实际命中(如果未命中,很可能遗漏了
--inspect-brk或在执行完成后才附加) - 暂停时的源码列表显示正确的文件(不匹配 = sourcemap 问题,见陷阱 1)
-
repl中的exec process.pid返回你意图附加的 PID
一次性配方
“为什么这个变量在第 X 行是 undefined?”
node --inspect-brk script.js &
node inspect -p $!
# debug>
sb('script.js', X)
cont
# 已暂停。现在:
repl
> myVariable
> Object.keys(this)“进入这个函数的调用路径是什么?”
debug> sb('suspectFn')
debug> cont
# 在入口处暂停
debug> bt
“这个异步链挂起了——在哪里?”
# 使用 --inspect 启动(不加 -brk),让它运行到挂起点,然后:
debug> pause
debug> bt
# 现在你会看到卡住的帧