【问题标题】:Expected variables not defined when using node inspect?使用节点检查时未定义预期变量?
【发布时间】:2018-09-22 23:46:15
【问题描述】:

我正在尝试熟悉使用node inspect 命令进行调试。我写了一个简单的脚本use_debugger.js,它定义了一个函数并调用它:

function count(nums, maxes) {
  debugger;
  return maxes;
}

count([1, 2, 3], [4, 5]);

当我进入调试器时,我希望nums 被定义并具有[1, 2, 3] 的值。但是,如果我运行 node inspect use_debugger.js 然后在 REPL 中输入 cnums,我会发现它没有定义:

Kurts-MacBook-Pro:Scratch kurtpeek$ node --inspect-brk use_debugger.js
Debugger listening on ws://127.0.0.1:9229/7adfaada-c939-44a3-9150-6d1326b8c7d0
For help, see: https://nodejs.org/en/docs/inspector
^C
Kurts-MacBook-Pro:Scratch kurtpeek$ node inspect use_debugger.js
< Debugger listening on ws://127.0.0.1:9229/54fb6a12-82c8-4454-8307-6d120b0c26e8
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in use_debugger.js:1
> 1 (function (exports, require, module, __filename, __dirname) { function count(nums, maxes) {
  2   debugger;
  3   return maxes;
debug> c
break in use_debugger.js:2
  1 (function (exports, require, module, __filename, __dirname) { function count(nums, maxes) {
> 2   debugger;
  3   return maxes;
  4 }
debug> maxes
repl:1
maxes
^

ReferenceError: maxes is not defined
    at repl:1:1
    at Script.runInContext (vm.js:101:20)
    at Object.runInContext (vm.js:279:6)
    at REPLServer.controlEval (internal/deps/node-inspect/lib/internal/inspect_repl.js:521:25)
    at bound (domain.js:396:14)
    at REPLServer.runBound [as eval] (domain.js:409:12)
    at REPLServer.onLine (repl.js:621:10)
    at REPLServer.emit (events.js:182:13)
    at REPLServer.EventEmitter.emit (domain.js:442:20)
    at REPLServer.Interface._onLine (readline.js:290:10)
debug> 

我是否应该不能“访问”函数范围内的变量,numsmaxes

更新

要进一步记录estus' 的答案,exec maxes 命令对我有用:

debug> exec maxes
[ 4, 5 ]

https://nodejs.org/api/debugger.html#debugger_information 中所述,exec expr 命令在调试脚本的上下文中执行一个表达式。

【问题讨论】:

    标签: javascript node.js debugging


    【解决方案1】:
    maxes
    

    在 REPL 范围内评估 maxes,而不是在 count 函数范围内。

    the documentation中所述:

    repl - 打开调试器的 repl 以在调试脚本的上下文中进行评估

    exec expr - 在调试脚本的上下文中执行表达式

    要在调试范围内评估maxes,它应该是:

    repl
    maxes
    

    或者:

    exec maxes
    

    【讨论】:

    • 调试时,eval 不会评估调试脚本上下文中的代码。
    • @AndrésAndrade 感谢您的注意,这是一个错字,它是exec
    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2018-09-09
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多