繁体   English   中英

使用非javascript进行nodejs调试

[英]nodejs debugging with non-javascript

就我而言,我想调试 CoffeeScript,但这个问题不是 CoffeeScript 特定的。 它涉及调试任何编译为 JavaScript 的非 JavaScript 语言。 为此,您需要创建 nodejs 能够执行的源映射。 但是,我想查看并逐步检查我的 CoffeeScript 代码中的变量,因为我知道 nodejs 实际上会执行相应的 JavaScript 代码——使用源映射映射它们之间的行号。 毫无疑问,有人会询问我的源代码,所以我们就使用它(称为 temp.coffee):

orders = [341, 454, 198, 264, 307]
totalOrders = 0
debugger
for order in orders
    totalOrders += order
console.log totalOrders

请注意,我添加了一个“调试器”语句,尽管这不是必需的 - 默认情况下,nodejs 应该在执行第一条语句之前停止。 通过在终端中执行此操作,我实际上已经接近实现我的目标:

coffee -cm temp.coffee
node inspect temp.coffee

第一条语句将 temp.coffee 编译为 temp.js,同时生成文件 temp.js.map - 一个源映射文件。 第二条语句似乎也有效 - 它告诉我调试器已附加并给我一个提示。 不幸的是,如果我尝试执行 's' 命令以跳过程序中的下一条语句,它会告诉我“未捕获的错误 [ERR_DEBUGGER_ERROR]:只能在暂停时执行操作。” 为什么不暂停??? 我试过使用“运行”或“重启”命令,但得到了相同的结果。 我希望我只是忘记做某事,并且有人可以提示我。

您的咖啡文件未作为节点脚本加载可能是因为它不是有效的文件扩展名。 您可以使用scripts命令观察这一点

$ node inspect temp.coffee
< Debugger listening on ws://127.0.0.1:9229/b8f44e67-51c6-428b-8010-ccc02a30459a
< For help, see: https://nodejs.org/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
< Waiting for the debugger to disconnect...
<
debug> scripts
  23: node:events
  24: node:buffer
  35: node:async_hooks
  37: node:stream
  41: node:util
  43: node:timers
  55: node:string_decoder
  58: node:stream/promises
  63: node:path
  65: node:fs
  68: node:querystring
  73: node:v8
  76: node:vm
  77: node:url
  80: node:crypto
  138: node:net
debug>

请注意任何脚本中都缺少任何temp名称。 您的调试文件应该是加载的脚本。


运行node inspect temp.js会产生以下结果

$ node inspect temp.js
< Debugger listening on ws://127.0.0.1:9229/658d30f3-c2d7-4524-8353-7974cea3048a
< For help, see: https://nodejs.org/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
Break on start in C:\workdir\temp\temp.js:2
  1 // Generated by CoffeeScript 2.7.0
> 2 (function() {
  3   var i, len, order, orders, totalOrders;
  4
debug> scripts
  23: node:events
  24: node:buffer
  35: node:async_hooks
  37: node:stream
  41: node:util
  43: node:timers
  55: node:string_decoder
  58: node:stream/promises
  63: node:path
  65: node:fs
  68: node:querystring
  73: node:v8
  76: node:vm
  77: node:url
  80: node:crypto
  138: node:net
* 153: C:\workdir\temp\temp.js
debug>

请注意对temp.js加载的引用和断点命中的显式注释。 然后你可以执行一些命令:

debug> c
break in C:\workdir\temp\temp.js:9
  7   totalOrders = 0;
  8
> 9   debugger;
 10
 11   for (i = 0, len = orders.length; i < len; i++) {
debug> exec totalOrders
0
debug> exec orders
[ 341,
  454,
  198,
  264,
  307 ]
debug> c
< 1564
<
< Waiting for the debugger to disconnect...
<
debug>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM