使用node-inspector调试Grunt任务

102

有人使用过node-inspectorGrunt进行应用程序调试吗?如果没有,你能推荐一个基于Grunt的应用程序调试工具吗?

我正在使用nodejs开发服务器端应用,同时也使用Grunt来执行分离的任务(这是因为用户可以单独执行任务)。


console.log是你的好朋友。我很想使用node-inspector,但我认为调试工具不是V8的一部分。据我所知,调试工具是一个独立的Web应用程序。如果我理解有误,请纠正我,因为我也想尝试你正在尝试的事情。 - iancrowther
是的,日志系统(我的意思是console.log或其他类型的日志机制)将永远是我们的朋友,但我需要的是一种不同且更易于调试的方式。到目前为止,我发现了一些grunt项目中缺少的要求,所以我已经将其移除,并且现在使用nodejs自身来进行调试。我知道这不是解决方案,但它有效。我认为,在未来,我会再次添加grunt,并加入node-inspector和其他工具/功能。最后,grunt真的很棒!我在其他项目中也在使用它,效果非常好! - JuanO
刚开始尝试使用Grunt玩耍,也对那个问题感兴趣... - Dmitry Pashkevich
@iancrowther,Web检查器使用的工具是V8。有一个名为“node-inspector”的项目,它与“node --debug”通信,将调试信息提供给连接的浏览器。这很棒,因为您可以将Chrome连接到node-inspector进程,并使用所有Web检查器工具来调试应用程序。https://github.com/dannycoates/node-inspector - David Souther
7个回答

135

要在调试模式下运行grunt,您需要显式地将grunt脚本传递给node:

node-debug $(which grunt) task

在你的任务中添加debugger;代码行。 node-inspector 将打开带有调试工具的浏览器。

编辑 2014 年 2 月 28 日

node-inspector 添加了命令 node-debug,它会以 --debug 状态启动 node 并在浏览器中打开 node-inspector 页面,在第一个 debugger 行或断点处停止。

编辑 2015 年 1 月 30 日

在 Windows 上,情况会稍微复杂一些。请参阅 @e.gluhotorenko 的答案以获取说明。


1
这太棒了。它还适用于其他全局安装的npm二进制文件。 - pllee
1
node debug $(which grunt) 任务使用了 Node.js 的默认调试器 - Tomas Romero
3
我对这个有点困惑。你能解释一下$(which grunt)是什么吗?它只是我想要调试的Gruntfile.js吗?在task中,我需要输入(以serve为例)grunt serve还是仅仅是serve?我尝试过node-debug Gruntfile.js serve和很多其他方法,但我就是无法让它工作。Node-inspector打开并且在第一行处停了下来,但即使在函数的第一行放置了一个debugger;行,我也不能进入serve任务。 - Brett
2
@brett,你用的是Windows还是OSX/Linux?在Unix上,$(which grunt)会被grunt-cli grunt.js脚本的完整路径所替换。这个脚本实际上运行了Grunt。如果你用的是Windows,请参考https://dev59.com/PWgu5IYBdhLWcg3wpYrJ#13443026(下面的答案)可能是grunt-cli脚本的位置。它将取代你命令中的`grunt` - 在你的例子中,将task替换为serve - David Souther
谢谢@DavidSouther,你想在你的回答中添加Windows特定部分吗?我真的应该转移到Linux... - Brett

39

Windows解决方案

运行

node --debug-brk c:\Users\username\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt taskname

在带有Gruntfile.js的目录中使用命令行。不要忘记在必要的地方放置debugger;代码行。


3
从grunt 0.4开始,grunt入口点是grunt-cli包的一部分:node --debug-brk [..]\node_modules\grunt-cli\bin\grunt - mistaecko
4
非常感谢您提供的Windows指令。在PowerShell中,您可以使用波浪号来使其更加稳定:node --debug-brk (Resolve-Path ~\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt) taskname - George Mauer
哎呀,我刚刚花了30分钟才搞明白它,然后更新了我的答案!我应该直接向下滚动的! - David Souther
1
不要忘记打开另一个控制台并运行 node-inspector - valter.santos.matos
@valter.santos.matos,现在你应该使用下面提到的node --inspect。 - Jackie

7
为了调试,我们需要修改bin下的grunt文件。在我的机器上,grunt是全局安装的,所以我进入了/usr/local/lib/node_modules/grunt/bin。我打开了文件并进行了修改: #!/usr/bin/env node 改为 #!/usr/bin/env node --debug-brk --debug-brk会在运行的第一行JavaScript上中断。
仅仅做到这些还不够,因为你无法在node检查器的下拉列表中找到你的grunt任务js文件,所以你需要通过添加debugger;来修改你想要调试的文件,在你想要中断的位置。现在你可以在第一个中断后点击继续,然后你将在你的debugger;line处中断。
相当粗糙,但这是我迄今发现的唯一方法。

2
这非常笨拙,因为它依赖于 /usr/bin/env 中的各种行为和 shebang 行中 # 和 \n 之间的未记录魔法。 - David Souther
2
它对我没用。Grunt仍然在没有调试模式的情况下运行。 - Eugene Gluhotorenko
@DavidSouther Shebang不是未记录的魔法。它们是标准化的POSIX行为。 - Miles Rout

6

你在想,运行 node --debug-brk $(which grunt) node-inspector build test 这样的东西?我喜欢。 - David Souther
基本上一样,只是我使用了并发,因为 node-inspector 不会退出并允许 buildtest 运行,因为它会永远运行。我添加了一个链接到一个片段,其中我已经设置好了它。 - ChrisWren
嗯,我再想想...我的工作流程是只需启动 node-inspector&,将其留在后台,并调用它完成。你对此有什么想法吗? - David Souther
1
不确定为什么,但这对我似乎不起作用。我运行命令 grunt node-inspector,然后转到本地主机URL,但我看不到任何源文件。只有空的调试工具。 - khollenbeck
我成功地运行了两个单独的命令来显示源代码。在我的grunt目录中运行grunt node-inspectornode --debug-brk Gruntfile.js。然而,当我在grunt文件中设置断点时,它会崩溃并显示一个连接错误。 - khollenbeck

4

我已经完成了一个任务,运行我的应用并启动node-inspector。 这比当前的建议要好得多,你只需要在gruntfile中添加这个任务:

  grunt.registerTask('debug', 'My debug task.', function() {
        var done = this.async();
        grunt.util.spawn({
            cmd: 'node',
            args: ['--debug', 'app.js'],
            opts: {
                //cwd: current workin directory
            }
        },
        function (error, result, code) {
            if (error) {
                grunt.log.write (result);
                grunt.fail.fatal(error);
            }
            done();
        });
        grunt.log.writeln ('node started');
        grunt.util.spawn({
            cmd: 'node-inspector',
            args: ['&'],
            opts: {
                //cwd: current workin directory
            }
        },
        function (error, result, code) {
            if (error) {
                grunt.log.write (result);
                grunt.fail.fatal(error);
            }
            done();
        });
        grunt.log.writeln ('inspector started');
    });

1
grunt-node-inspector 的作者是正确的。这个解决方案只适用于 posix 环境,而他的方案更容易配置 node-inspector,并且可以在 posix 和 windows 上并行工作。 - rainabba

3

这里有很好的答案。2017年,现在你可以执行

node --inspect --debug-brk $(which grunt) taskName

这会输出类似下面的内容。

要开始调试,请在Chrome中打开以下URL: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/232652c3-f63c-4b00-8de9-17dfad5db471

在Chrome中打开该URL,您就可以开始了!

我正在使用Node 7.3.0,并且我使用的是Mac。 您可能需要遵循其他帖子中的建议才能在Windows上运行它。


2

2019更新

  • 如果您想以调试模式启动grunt任务并在第一行中断:

    node --inspect-brk $(which grunt) 任务名称

  • 如果您想以特定端口的调试模式启动grunt任务:

    node --inspect-brk=8080 $(which grunt) 任务名称

  • 如果您想将VSCODE附加到运行grunt调试会话的节点进程,请在vscode中使用以下配置:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    
    {
      "type": "node",
      "request": "attach",
      "name": "Attach by port IP 5656",
      "port": 8080
    }
  ]
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接