Node Inspect Debugger
通过 --inspect + Chrome DevTools Protocol 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 |
参考:完整 SKILL.md
以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。
Node.js Inspect Debugger
概述
当 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
# or with tsx
node --inspect-brk $(which tsx) path/to/script.ts
debug> 提示符接受以下命令:
| 命令 | 操作 |
|---|---|
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') | 每次暂停时计算 expr |
watchers | 显示被监视的表达式 |
repl | 进入当前作用域的 REPL(按 Ctrl+C 退出 REPL) |
exec expr | 计算一次表达式 |
restart | 重启脚本 |
kill | 终止脚本 |
.exit | 退出调试器 |
在 repl 子模式下: 输入任何 JS 表达式,包括访问局部变量/闭包变量。按 Ctrl+C 返回 debug>。
附加到正在运行的进程
当进程已经在运行时(例如长期运行的开发服务器或 TUI 网关):
# 1. Send SIGUSR1 to enable the inspector on an existing process
kill -SIGUSR1 <pid>
# Node prints: Debugger listening on ws://127.0.0.1:9229/<uuid>
# 2. Attach the debugger CLI
node inspect -p <pid>
# or by URL
node inspect ws://127.0.0.1:9229/<uuid>
从头开始启动带有检查器的进程:
node --inspect script.js # listen on 127.0.0.1:9229, keep running
node --inspect-brk script.js # listen AND pause on first line
node --inspect=0.0.0.0:9230 script.js # custom host:port
对于通过 tsx 运行的 TypeScript:
node --inspect-brk --import tsx script.ts
# or older tsx
node --inspect-brk -r tsx/cjs script.ts
编程式 CDP(从终端进行脚本化)
当你想要自动化操作时——设置多个断点、捕获作用域状态、编写可复现的脚本——使用 chrome-remote-interface:
npm i -g chrome-remote-interface # or project-local
# Start your target:
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}`);
// Walk scopes for locals
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);
}
}
}
// Evaluate an expression in the paused frame
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();
// Set a breakpoint by URL regex + line
await Debugger.setBreakpointByUrl({
urlRegex: '.*app\\.tsx
运行它:
```bash
node /tmp/cdp-debug.js
Hermes 特定说明:ui-tui/package.json 中不包含 chrome-remote-interface。如果你不想弄乱项目,可以将其安装到临时位置:
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 # produce dist/ once so transpile isn't needed on first load
node --inspect-brk dist/entry.js
# In another terminal: \{#debugging-hermes-ui-tui}
node inspect -p <node pid>
然后在 debug> 内部:
sb('dist/app.js', 220) # or wherever the suspect render is
cont
当它暂停时,进入 repl → 检查 props、状态引用、useInput 处理程序值等。
调试正在运行的 hermes --tui
TUI 由 Python CLI 生成 Node 进程。最简单的路径:
# 1. Launch TUI \{#debugging-a-single-ink-component-under-dev}
hermes --tui &
TUI_PID=$(pgrep -f 'ui-tui/dist/entry' | head -1)
# 2. Enable inspector on that Node PID \{#debugging-a-running-hermes---tui}
kill -SIGUSR1 "$TUI_PID"
# 3. Find the WS URL \{#debugging-_slashworker--pty-child-processes}
curl -s http://127.0.0.1:9229/json/list | jq -r '.[0].webSocketDebuggerUrl'
# 4. Attach \{#running-vitest-tests-under-the-debugger}
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-run 测试)使用此技能。
在调试器下运行 Vitest 测试
cd /home/bb/hermes-agent/ui-tui
# Run a single test file paused on entry \{#heap-snapshots--cpu-profiles-non-interactive}
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:
// CPU profile for 5 seconds
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));
// Open /tmp/cpu.cpuprofile in Chrome DevTools → Performance tab
// Heap snapshot
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 # lists all inspectable targets on the host -
子进程。 父进程上的
--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返回 exactly 你期望的目标 - 第一个断点确实命中(如果没有命中,你可能遗漏了
--inspect-brk或在执行完成后才附加) - 暂停时的源码列表显示正确的文件(不匹配 = sourcemap 问题,参见陷阱 1)
- 在
repl中执行exec process.pid返回你打算附加的 PID
一次性方案
“为什么第 X 行的这个变量是 undefined?”
node --inspect-brk script.js &
node inspect -p $!
# debug> \{#common-pitfalls}
sb('script.js', X)
cont
# paused. Now: \{#verification-checklist}
repl
> myVariable
> Object.keys(this)
“进入此函数的调用路径是什么?”
debug> sb('suspectFn')
debug> cont
# paused on entry \{#one-shot-recipes}
debug> bt
“这个异步链挂起了——在哪里?”
# Start with --inspect (no -brk), let it run to the hang, then:
debug> pause
debug> bt
# Now you see the stuck frame
```,
lineNumber: 119, // 0-indexed
columnNumber: 0,
});
await Runtime.runIfWaitingForDebugger();
})();
运行它:
node /tmp/cdp-debug.js
Hermes 特定说明:ui-tui/package.json 中不包含 chrome-remote-interface。如果你不想弄乱项目,可以将其安装到临时位置:
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 # produce dist/ once so transpile isn't needed on first load
node --inspect-brk dist/entry.js
# In another terminal:
node inspect -p <node pid>
然后在 debug> 内部:
sb('dist/app.js', 220) # or wherever the suspect render is
cont
当它暂停时,进入 repl → 检查 props、状态引用、useInput 处理程序值等。
调试正在运行的 hermes --tui
TUI 由 Python CLI 生成 Node 进程。最简单的路径:
# 1. Launch TUI
hermes --tui &
TUI_PID=$(pgrep -f 'ui-tui/dist/entry' | head -1)
# 2. Enable inspector on that Node PID
kill -SIGUSR1 "$TUI_PID"
# 3. Find the WS URL
curl -s http://127.0.0.1:9229/json/list | jq -r '.[0].webSocketDebuggerUrl'
# 4. Attach
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-run 测试)使用此技能。
在调试器下运行 Vitest 测试
cd /home/bb/hermes-agent/ui-tui
# Run a single test file paused on entry
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:
// CPU profile for 5 seconds
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));
// Open /tmp/cpu.cpuprofile in Chrome DevTools → Performance tab
// Heap snapshot
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 # lists all inspectable targets on the host -
子进程。 父进程上的
--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返回 exactly 你期望的目标 - 第一个断点确实命中(如果没有命中,你可能遗漏了
--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
# paused. Now:
repl
> myVariable
> Object.keys(this)
“进入此函数的调用路径是什么?”
debug> sb('suspectFn')
debug> cont
# paused on entry
debug> bt
“这个异步链挂起了——在哪里?”
# Start with --inspect (no -brk), let it run to the hang, then:
debug> pause
debug> bt
# Now you see the stuck frame