将节点检查器与Grunt任务一起使用

Jua*_*anO 101 javascript debugging node.js node-inspector gruntjs

是否有人使用节点检查咕噜为应用程序调试?如果没有,你能推荐一个基于Grunt的应用程序的调试工具吗?

我正在使用nodejs作为服务器端应用程序,我让Grunt使用分离的任务(这是因为用户可以单独执行任务).

Dav*_*her 135

要在调试中运行grunt,您需要将grunt脚本显式传递给节点:

node-debug $(which grunt) task
Run Code Online (Sandbox Code Playgroud)

debugger;在你的任务中加入一条线.node-inspector然后将打开带有调试工具的浏览器.

编辑2014年2月28日

node-inspector添加了命令node-debug,它在一个--debug状态下启动节点并打开浏览器到node-inspector页面,当它到达第一debugger行或设置断点时停止.

编辑2015年1月30日

在Windows上,事情变得更加复杂.有关说明,请参阅@ egluhotorenko的答案.

  • 我有点麻烦.你能澄清一下`$(哪个grunt)`是什么?它只是我要调试的Gruntfile.js吗?对于`task`,我把(使用`serve`作为例子)`grunt serve`或者只是'serve`?我已经尝试过`node-debug Gruntfile.js serve`以及其他很多东西,但我无法让它工作.节点检查器打开并在第一行中断,但即使我将`debugger;`行作为函数的第一行,我也无法进入`serve`任务. (3认同)
  • @brett,你在Windows或OSX/Linux上吗?在unix上,$(grunt)被grunt-cli`grunt.js`脚本的完整路径取代.这是实际运行Grunt的脚本.如果你在Windows上,请查看http://stackoverflow.com/a/13443026/240358(下面的答案),了解grunt-cli脚本的可能位置.这取代了你的命令中的`grunt` - 在你的例子中,用`serve`替换`task` (2认同)

Eug*_*nko 39

Windows解决方案

node --debug-brk c:\Users\username\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt taskname
Run Code Online (Sandbox Code Playgroud)

来自你的目录中的cmd Gruntfile.js.别忘了把debugger;线放在必要的地方.

  • 非常感谢Windows指令.在powershell中你可以使用代字号来使它变得不那么脆弱`node --debug-brk(Resolve-Path~\AppData\Roaming \npm \node_modules\grunt-cli\bin\grunt)taskname` (4认同)
  • 从grunt 0.4开始,grunt入口点是`grunt-cli`包的一部分:`node --debug-brk [..] \node_modules\grunt-cli\bin\grunt` (3认同)

小智 7

要进行调试,我们必须修改bin下的grunt文件.在我的机器上,grunt是全局安装的,所以我去了/ usr/local/lib/node_modules/grunt/bin我打开文件并修改:

#!/usr/bin/env node

#!/usr/bin/env node --debug-brk

--debug-brk将在javascript运行的第一行中断.

单独这样做是不够的,因为你将无法在节点检查器的下拉列表中找到你的grunt task js文件,所以你必须通过添加debugger;where 来修改你感兴趣的文件.你想要断点发生.现在你可以在第一次休息后点击继续,你就会突破你的debugger;

非常kludgy,但这是我到目前为止找到的唯一方法.

  • 这是令人难以置信的kludgy,因为它依赖于/ usr/bin/env中的各种行为以及shebang行中#和\n之间的无证魔法. (2认同)
  • 它对我不起作用.Grunt仍然在没有调试模式的情况下运行 (2认同)

Chr*_*ren 6

我最近创建了grunt-node-inspector来轻松配置节点检查器和其他grunt工作流程,请查看:https://github.com/ChrisWren/grunt-node-inspector

下面是Grunt文件的一部分,它说明了如何使用grunt-node-inspector,grunt-concurrent和grunt-shell调试grunt任务:https://github.com/CabinJS/Cabin/blob/master/Gruntfile. JS#L44-L77