



















@@ -0,0 +1,85 @@
1+---
2+name: node-inspect-debugger
3+description: Debug Node.js with node inspect, --inspect, breakpoints, CDP, heap, and CPU profiles.
4+metadata: { "openclaw": { "emoji": "🪲", "requires": { "bins": ["node"] } } }
5+---
6+7+# Node Inspect Debugger
8+9+Use when logs are too weak for a Node.js bug: hidden locals, async hangs, flaky tests, child processes, startup races, memory growth, or CPU hot paths.
10+11+Default to `node inspect` first. Use Chrome DevTools Protocol only when you need scripted breakpoints, automated state capture, heap snapshots, or CPU profiles.
12+13+Quick start
14+15+- Pause on entry: `node inspect path/to/script.js`
16+- TypeScript: `node --inspect-brk --import tsx path/to/script.ts`
17+- Existing PID: `kill -SIGUSR1 <pid>` then `node inspect -p <pid>`
18+- Inspect target list: `curl -s http://127.0.0.1:9229/json/list | jq`
19+- OpenClaw CLI path: `node --inspect-brk openclaw.mjs ...`
20+- OpenClaw test path: `OPENCLAW_VITEST_MAX_WORKERS=1 node --inspect-brk scripts/run-vitest.mjs <file>`
21+22+Debugger REPL
23+24+- Continue/step: `cont`, `next`, `step`, `out`, `pause`
25+- Breakpoints: `sb('file.js', 42)`, `sb(42)`, `sb('functionName')`, `breakpoints`, `cb('file.js', 42)`
26+- Inspect: `bt`, `list(8)`, `watch('expr')`, `exec expr`
27+- Current scope: `repl`, then evaluate locals directly; `Ctrl+C` exits repl mode.
28+- Exit safely: `cont` before quitting if the process should continue; otherwise `kill`.
29+30+OpenClaw tips
31+32+- Prefer `127.0.0.1` inspector binds. Do not expose `--inspect=0.0.0.0` unless the network is isolated.
33+- For Vitest, debug one file with one worker. Avoid worker pools while stepping.
34+- For TS source breakpoints, use `--enable-source-maps` when useful; `node inspect` can still show emitted paths.
35+- For child processes, `NODE_OPTIONS=--inspect-brk` can propagate the inspector, but each child needs its own port.
36+- For long-lived gateway or dev processes, attach by PID after confirming the target with `/json/list`.
37+38+Programmatic CDP
39+40+Install tooling outside the repo unless the project already depends on it:
41+42+```bash
43+mkdir -p /tmp/cdp-tools
44+npm --prefix /tmp/cdp-tools i chrome-remote-interface
45+NODE_PATH=/tmp/cdp-tools/node_modules node /tmp/cdp-debug.cjs
46+```
47+48+Minimal driver:
49+50+```js
51+const CDP = require("chrome-remote-interface");
52+53+(async () => {
54+const client = await CDP({ port: 9229 });
55+const { Debugger, Runtime } = client;
56+57+Debugger.paused(async ({ callFrames, reason }) => {
58+const top = callFrames[0];
59+console.log("paused", reason, top.url, top.location.lineNumber + 1);
60+const { result } = await Debugger.evaluateOnCallFrame({
61+ callFrameId: top.callFrameId,
62+ expression: "JSON.stringify({ pid: process.pid })",
63+ });
64+console.log(result.value ?? result.description);
65+await Debugger.resume();
66+ });
67+68+await Runtime.enable();
69+await Debugger.enable();
70+await Debugger.setBreakpointByUrl({ urlRegex: ".*target\\.js$", lineNumber: 41 });
71+await Runtime.runIfWaitingForDebugger();
72+})();
73+```
74+75+Profiles
76+77+- CPU: enable `Profiler`, `start`, wait, `stop`, write `/tmp/profile.cpuprofile`, open in Chrome DevTools.
78+- Heap: enable `HeapProfiler`, collect `addHeapSnapshotChunk`, call `takeHeapSnapshot`, write `/tmp/heap.heapsnapshot`.
79+80+Pitfalls
81+82+- `--inspect` does not pause; use `--inspect-brk` when setup must happen before code runs.
83+- Default port is `9229`; use `--inspect=0` or a unique port for parallel targets.
84+- If a breakpoint misses, confirm file path, source map behavior, and whether execution already passed the line.
85+- If the process appears frozen after detaching, it may still be paused in the debugger.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。