【问题标题】:How to I view a variable when debugging in Node.js?在 Node.js 中调试时如何查看变量?
【发布时间】:2019-01-27 15:19:15
【问题描述】:

Node.js 调试器隐藏了我的大部分字符串变量,而是打印 ...,如下所示:(有一个名为 source 的变量)

$ node debug 127.0.0.1:9101
debug> 
debug> exec('source')
'<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" class="DW dw-pri js no... (length: 6408)'
debug>
debug> repl
Press Ctrl + C to leave debug repl
> source
'<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" class="DW dw-pri js no... (length: 6408)'

如您所见,它用... 替换了大部分字符串。我用谷歌搜索了一段时间并在控制台中输入了help。最后在这里发布——如何查看字符串的完整内容? console.log(source) 什么都没做。

编辑: 现在我注意到 console.log 确实在 Selenium 的终端窗口中打印了变量,这也许是我应该预料到的。不确定console.log 并切换到 Selenium 终端窗口是否是我应该做的

更新:

在repl 之外只输入source 不起作用:

debug> source
ReferenceError: source is not defined
    at repl:1:1
    at ContextifyScript.Script.runInContext (vm.js:32:29)
    at Object.runInContext (vm.js:87:6)
    at Interface.controlEval (_debugger.js:971:21)
    at REPLServer.eval (_debugger.js:745:41)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:536:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:191:7)

(当在 repl 中时,变量会被截断,如上面最上面的 sn-p 所示。)

还有print(source) 或print('source'):

debug> print('source')
ReferenceError: print is not defined
    at repl:1:1
    at ContextifyScript.Script.runInContext (vm.js:32:29)
    at Object.runInContext (vm.js:87:6)
    at Interface.controlEval (_debugger.js:971:21)
    at REPLServer.eval (_debugger.js:745:41)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:536:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:191:7)
debug> repl
Press Ctrl + C to leave debug repl
> print(source)
ReferenceError: print is not defined

可供选择的命令不多:(在调试器中)

debug> help
Commands: run (r), cont (c), next (n), step (s),
out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, exec, restart, kill, list, scripts,
breakOnException, breakpoints, version

【问题讨论】:

  • 您是否尝试过在调试命令行中仅输入变量名称而不使用console.log()?
  • 用print 代替exec 怎么样?
  • 嗨@Soviut,我测试了只输入source。那么调试器将找不到source,因为source变量只在Node.js被调试进程中可用,而在调试器本身中不可用。
  • 嗨@OmG,我测试了print——没有这样的功能可用(不在调试器中,不在repl中)
  • 有什么原因你没有使用 devtools?

标签: node.js debugging


【解决方案1】:

watch("your_var_name") 将打印您在每个断点处观察的变量的值(您必须将变量/表达式名称放在引号中)。

这似乎是命令行上最接近的东西,虽然坦率地说只是写

console.log(thing I want to log); debugger;

对我来说感觉更轻松。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2019-01-24
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 2010-12-26
    相关资源
    最近更新 更多