选择探头位置
🌐 Selecting the probe location
当执行到探测位置时,这些表达式会在探测位置的词法作用域中进行求值。避免在由 let 或 const 声明的变量的声明位置进行探测,因为这会导致 ReferenceError,这是由于在变量的暂时性死区(TDZ)中访问该变量所引起的。
🌐 The expressions are evaluated in the lexical scope of the probe location when
execution reaches it. Avoid probing a variable declared by let or const at its
declaration site, as this leads to a ReferenceError caused by
accessing the variable in its temporal dead zone (TDZ).
// app.js
const x = 42; // line 2
console.log(x); // line 3 $ node inspect --probe app.js:1 --expr 'x' app.js
Hit 1 at file:///path/to/app.js:1:1
[error] x = ReferenceError: Cannot access 'x' from debugger
...
Completed 相反,在变量已经被初始化的位置进行探测:
🌐 Instead, probe at a location where the variable is already initialized:
$ node inspect --probe app.js:3 --expr 'x' app.js
Hit 1 at file:///path/to/app.js:3:1
x = 42
Completed <file> 参数会作为每个已加载脚本 URL 的路径后缀进行匹配,并以路径分隔符为锚点。仅传递文件名会匹配每个具有该文件名的已加载脚本,这类似于本地调试器通常匹配断点的方式,而传递部分路径则会缩小匹配范围。给定:
🌐 The <file> argument is matched as a path suffix of every loaded
script URL, anchored on a path separator. Passing only a basename
matches every loaded script with that basename, similar to how native
debuggers typically match breakpoints, while passing a partial path
narrows the match. Given:
project/
- src/utils.js
- lib/utils.js --probe utils.js:10 绑定到 两个 文件,并为每个匹配项生成一次命中。
每个命中都带有自己的 location 字段,用于标识表达式实际执行的位置,因此使用者可以准确地将结果归属到两个文件中的一个。为了在绑定时消除歧义,请指定一个只匹配目标文件的完整路径:
$ node inspect --probe src/utils.js:10 --expr 'x' main.js # matches only src/utils.js