Javascript 在 Grunt 任务中使用节点检查器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11171013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:44:54  来源:igfitidea点击:

Using node-inspector with Grunt tasks

javascriptdebuggingnode.jsnode-inspectorgruntjs

提问by JuanO

Does someone used node-inspectorwith Gruntfor application debugging? If not, Can you recommend a debugging tool for Grunt based apps?

有人使用带有Grunt 的节点检查器进行应用程序调试吗?如果没有,您能否为基于 Grunt 的应用程序推荐一个调试工具?

I'm working with nodejsfor a server side app and I have Gruntto use separated tasks (this is because users can execute tasks separately).

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

回答by David Souther

To run grunt in debug, you need to pass the grunt script to node explicitly:

要在调试中运行 grunt,您需要明确地将 grunt 脚本传递给 node:

node-debug $(which grunt) task

and put a debugger;line in your task. node-inspectorwill then open a browser with debugging tools.

debugger;在您的任务中添加一行。node-inspector然后将打开带有调试工具的浏览器。

Edit 28 Feb 2014

2014 年 2 月 28 日编辑

node-inspectorhas added the command node-debug, which launches node in a --debugstate and opens the browser to the node-inspectorpage, stopping when it hits the first debuggerline or set breakpoint.

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

Edit 30 January 2015

2015 年 1 月 30 日编辑

On Windows, things are a touch more complicated. See the answer from @e.gluhotorenko for instructions.

在 Windows 上,事情有点复杂。有关说明,请参阅@e.gluhotorenko 的答案。

回答by Eugene Gluhotorenko

Windows solution

视窗解决方案

Run

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

from cmd in the directory with your Gruntfile.js. Do not forget to put debugger;line in necessary places.

从 cmd 与您的Gruntfile.js. 不要忘记把debugger;线放在必要的地方。

回答by user1577390

To debug, we have to modify the grunt file under bin. On my machine, grunt is installed globally, so I went to /usr/local/lib/node_modules/grunt/bin I opened the file and modified:

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

#!/usr/bin/env node

#!/usr/bin/env node

To

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

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

--debug-brk will break on the first line of javascript ran.

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

Doing that alone isn't quite enough though, since you won't be able to find you're grunt task js file in the drop down in node inspector, so you have to modify the file you're interested in debugging by adding debugger;where you want the breakpoint to happen. Now you can click continue after the first break, and you'll break on you're debugger;line

但是,单独这样做还不够,因为您将无法在节点检查器的下拉列表中找到 grunt 任务 js 文件,因此您必须通过添加debugger;where来修改您有兴趣调试的文件你希望断点发生。现在你可以在第一次休息后点击继续,你会在你的debugger;线上休息

Pretty kludgy, but it's the only way I've found so far.

很笨拙,但这是我迄今为止找到的唯一方法。

回答by ChrisWren

I recently created grunt-node-inspector to easily configure node-inspector with the rest of your grunt workflow, check it out: https://github.com/ChrisWren/grunt-node-inspector

我最近创建了 grunt-node-inspector 来轻松配置 node-inspector 与您的 grunt 工作流程的其余部分,请查看:https: //github.com/ChrisWren/grunt-node-inspector

Here is a section of a Gruntfile which illustrates how you can debug a grunt task using grunt-node-inspector, grunt-concurrent, and grunt-shell: https://github.com/CabinJS/Cabin/blob/master/Gruntfile.js#L44-L77

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

回答by olivier dufour

I have done a task to run my app and launch node-inspector. It is far better than current proposition, you just have to add this task in gruntfile:

我完成了一项任务来运行我的应用程序并启动节点检查器。它比当前的命题好得多,你只需要在 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');
    });

回答by RoccoB

Great answers here. In 2017, now you can do

很棒的答案在这里。2017年,现在你可以做到

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

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

Which prints something like.

打印出类似的东西。

To start debugging, open the following URL in Chrome: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/232652c3-f63c-4b00-8de9-17dfad5db471

To start debugging, open the following URL in Chrome: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/232652c3-f63c-4b00-8de9-17dfad5db471

Open that URL in chrome, and you're good to go!

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

I'm using Node 7.3.0 and I'm on Mac. You might have to follow some of the advice in other posts to get it going on Windows.

我使用的是 Node 7.3.0,我使用的是 Mac。您可能必须遵循其他帖子中的一些建议才能在 Windows 上运行。

回答by ZEE

2019 update

2019年更新

  • If you want to launch the grunt task in debug mode and break at first line:

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

  • If you want to launch the grunt task in debug mode at a specific port:

    node --inspect-brk=8080 $(which grunt) taskName

  • if you want to attache VSCODE to the node process running the debugging session of grunt, use the following configuration in vscode:

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

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

  • 如果要在特定端口以调试模式启动 grunt 任务:

    node --inspect-brk=8080 $(which grunt) taskName

  • 如果要将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
    }
  ]
}