【发布时间】:2020-05-25 06:41:39
【问题描述】:
在debugger 语句中断后,尝试调用foo 会抛出ReferenceError。该函数似乎没有在脚本的上下文或范围中定义,例如本地 x 变量。
example.js脚本:
/**
* Source code example
*/
const x = 'x'
let y
function foo(param = 'foo') {
console.log(param)
}
// const f = foo // foo throws error if commented out and referenced from debugger
debugger
用inspector监听启动节点进程:
node --inspect-brk example.js
内置 Node.js 调试器:
$ node inspect 127.0.0.1:9229
Break on start in scripts/example.js:5
3 */
4
> 5 const x = 'x'
6 let y
7
debug> c
break in scripts/example.js:14
12 // const f = foo // foo throws error if commented out and referenced from debugger
13
>14 debugger
15
debug> exec foo()
ReferenceError: foo is not defined
at eval (eval at <anonymous> (/path/to/scripts/example.js:14:1), <anonymous>:1:1)
at Object.<anonymous> (/path/to/scripts/example.js:14:1)
at Module._compile (internal/modules/cjs/loader.js:952:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
在 VS Code 的调试控制台中:
foo()
ReferenceError: foo is not defined
在 Chrome DevTools 的控制台中:
foo()
VM88:1 Uncaught ReferenceError: foo is not defined
at eval (eval at <anonymous> (/path/to/scripts/example.js:14:1), <anonymous>:1:1)
at Object.<anonymous> (/path/to/scripts/example.js:14:1)
at Module._compile (internal/modules/cjs/loader.js:952:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
我发现能够在一个调试会话中测试具有不同参数的函数而无需重新启动真的很有用。
【问题讨论】:
-
以
v10.16.3为例,可以引用该函数。在v13.11.0,仍然没有。
标签: javascript node.js debugging node-inspector