如何在Node.js中调试时查看变量?

时间:2017-06-20 04:20:50

标签: node.js debugging

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终端窗口,是我应该做的事情

更新

仅在source之外键入repl并不起作用:

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中,变量被截断,如上面最上面的代码片段所示。)

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

1 个答案:

答案 0 :(得分:0)

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

这似乎是命令行上最接近的内容,尽管坦率地说只是写

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

对我来说更容易。