简体   繁体   English

如何使用node-inspector,Windows 7调试带断点的Gruntfile?

[英]How to debug a Gruntfile with breakpoints using node-inspector, Windows 7?

So I have spent the past couple days trying to get this to work with no luck. 所以我花了几天的时间试图让这个没有运气。 Most of the solutions I have found seem to work "okay" for debugging node applications. 我发现的大多数解决方案对于调试节点应用程序似乎都“正常”。 But I haven't had much luck debugging grunt stand alone. 但我没有多少运气调试咕噜声独立。 I would like to be able to set breakpoints in my gruntfile and either step through the code with either the browser or an IDE. 我希望能够在我的gruntfile中设置断点,并使用浏览器或IDE逐步执行代码。

I have tried the following: 我尝试过以下方法:

ERROR MESSAGE USING NODE-INSPECTOR 使用NODE-INSPECTOR的错误消息

在此输入图像描述

So currently node-inspector feels like it has gotten me the closest to what I want. 因此,目前节点检查员感觉它已经让我最接近我想要的。 To get here I did the following: 到这里我做了以下事情:

From my grunt directory I ran the following commands: 从我的grunt目录中,我运行了以下命令:

grunt node-inspector
node --debug-brk Gruntfile.js

And then from there I went to localhost:8080/debug?port=5858 to debug my Gruntfile.js. 然后从那里我去localhost:8080/debug?port=5858来调试我的Gruntfile.js。 But like I mentioned above, as soon as I hit F8 to skip to breakpoint it crashes with the above error. 但就像我上面提到的那样,只要我点击F8跳到断点,它就会因上述错误而崩溃。 Has anybody had any success using this method to try to debug a Gruntfile? 有没有人使用这种方法尝试调试Gruntfile有什么成功? So far from my search efforts I have not found a very well documented way of doing this. 到目前为止,我的搜索工作还没有找到一个记录完备的方法。 So hopefully this will be useful or beneficial information for future users. 所以希望这对未来的用户来说是有用的或有益的信息。 Also I am using Windows 7 by the way. 我也顺便使用Windows 7 Thanks in advance. 提前致谢。

UPDATE: 更新:

I have tried the following suggested by @dylants with no luck so far. 到目前为止,我已经尝试了@dylants提出的以下建议。

  1. Found the grunt.cmd file on windows machine located in C:\\Users\\khollenbeck\\AppData\\Roaming\\npm . 在位于C:\\Users\\khollenbeck\\AppData\\Roaming\\npm Windows机器上找到grunt.cmd文件。 Opened up grunt.cmd file and found the following.... 打开grunt.cmd文件,发现以下内容....

    在此输入图像描述

  2. This lead me to C:\\Users\\khollenbeck\\AppData\\Roaming\\npm\\node_modules\\grunt-cli\\bin which contained a file called grunt . 这导致我到C:\\Users\\khollenbeck\\AppData\\Roaming\\npm\\node_modules\\grunt-cli\\bin ,其中包含一个名为grunt的文件。 And from there at the top of the file. 从那里的文件的顶部。 I changed this code #!/usr/bin/env node to #!/usr/bin/env node --debug-brk 我将此代码#!/usr/bin/env node更改为#!/usr/bin/env node --debug-brk

  3. After doing this command node-inspector C:\\path\\to\\gruntfile grunt I got the following. 执行此命令后, node-inspector C:\\path\\to\\gruntfile grunt我得到以下内容。

    Node Inspector v0.7.3 Visit http://localhost:8080/debug?port=5858 to start debugging

  4. Next I ran the grunt command from grunt dir. 接下来,我从grunt目录运行了grunt命令。 ( leaving the server running in original command prompt ) 让服务器在原始命令提示符下运行

From here I expected Gruntfile.js to show up in the source of chrome dev tools. 从这里我预计Gruntfile.js将出现在chrome dev工具的源代码中。 I also expected to be able to set breakpoints from there. 我也希望能够从那里设置断点。 This did not happen. 这没有发生。 Instead it just ran all the way through the Gruntfile without breaking or loading in the browser. 相反,它只是在Gruntfile中一直运行而不会在浏览器中中断或加载。

Edit: 编辑:

Ah, I see what I did wrong. 啊,我明白我做错了什么。 For some reason I did node --debug-brk with out adding the path C:\\Users\\khollenbeck\\AppData\\Roaming\\npm\\node_modules\\grunt-cli\\bin\\grunt. 出于某种原因,我做了节点--debug-brk而没有添加路径C:\\ Users \\ khollenbeck \\ AppData \\ Roaming \\ npm \\ node_modules \\ grunt-cli \\ bin \\ grunt。 Its working now, thanks so much. 它现在正在工作,非常感谢。 I apologize for dragging this on for so long. 我为拖延这么久而道歉。 But hopefully this will be useful for other windows users in the future. 但希望这对将来的其他Windows用户有用。

This can be accomplished by starting up node-inspector and starting grunt in debug mode. 这可以通过启动node-inspector并在调试模式下启动grunt来完成。 Once that's done, you can step through your Gruntfile.js within Chrome as you normally would. 完成后,您可以像往常一样Gruntfile.js浏览Chrome中的Gruntfile.js

start node-inspector 启动节点检查器

If you don't already have node-inspector , install it using npm install -g node-inspector . 如果您还没有node-inspector ,请使用npm install -g node-inspector进行npm install -g node-inspector Then start it up in one terminal/command prompt: 然后在一个终端/命令提示符下启动它:

$ node-inspector
Node Inspector v0.7.3
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

run grunt in debug mode 在调试模式下运行grunt

Next, locate your grunt script. 接下来,找到您的grunt脚本。 This is the JavaScript file that is executed when you run the grunt command from the command line. 这是从命令行运行grunt命令时执行的JavaScript文件。 If you installed grunt globally (using npm install -g grunt-cli ) then it will most likely be in /usr/bin or /usr/local/bin for *nix or Mac machines. 如果您全局安装了grunt(使用npm install -g grunt-cli ),那么它很可能位于/usr/bin/usr/local/bin用于* nix或Mac机器。 For Windows machines, the grunt.cmd file points to where the grunt script is located. 对于Windows机器, grunt.cmd文件指向grunt脚本所在的位置。 Most likely the grunt script is located in C:\\Users\\<username>\\AppData\\Roaming\\npm\\node_modules\\grunt-cli\\bin . 很可能grunt脚本位于C:\\Users\\<username>\\AppData\\Roaming\\npm\\node_modules\\grunt-cli\\bin

Once you've found the location of the script, use node --debug-brk to execute this script, thus starting grunt in debug mode breaking on the first line of code in the file. 找到脚本的位置后,使用node --debug-brk执行此脚本,从而在调试模式下启动grunt,打破文件中的第一行代码。 So for instance, imagine the grunt script is located at /usr/bin/grunt : 例如,假设grunt脚本位于/usr/bin/grunt

$ node --debug-brk /usr/bin/grunt 
debugger listening on port 5858

You'll know you're successful when you see debugger listening on port 5858 as the output, which means that the grunt script has halted execution and is waiting to be stepped through with the debugger. 当你看到debugger listening on port 5858作为输出时,你会知道你是成功的,这意味着grunt脚本已经暂停执行并等待调试器逐步执行。

debug with Chrome 使用Chrome调试

Now bring up Chrome and point it to http://127.0.0.1:8080/debug?port=5858 . 现在打开Chrome并将其指向http://127.0.0.1:8080/debug?port=5858 Within Chrome, open and add break points in your Gruntfile.js , and step through as you normally would. 在Chrome中,打开并在Gruntfile.js添加断点,然后像往常一样逐步完成。

To start the node-inspector you should no longer pass the --debug or --debug-brk flag. 要启动节点检查器,您不应再传递--debug或--debug-brk标志。 You can start it directly using node-inspector and a filepath, just like you would normally starting execute a script using the node command. 您可以使用node-inspector和文件路径直接启动它,就像您通常使用node命令开始执行脚本一样。 This should automatically open the webkit inspector in chrome, and pause at the first line of execution. 这应该在chrome中自动打开webkit检查器,并在第一行执行时暂停。 You can from here insert your breakpoints and debug as normal. 您可以从这里插入断点并正常调试。

As mentioned above, to debug a grunt task you would have to find your Grunt executable using $ which grunt in Mac, not sure about Windows. 如上所述,要调试grunt任务,你必须使用Mac中的$ which grunt找到你的Grunt可执行文件,不确定Windows。 Then you would have to copy that path, and use it as the first argument passed to node-debug, with the second argument being the task (and you could also include the grunt target if necessary). 然后你必须复制该路径,并将其用作传递给node-debug的第一个参数,第二个参数是任务(如果需要,你也可以包括grunt目标)。 So for example it may look like: 例如,它可能看起来像:

node-debug /usr/local/bin/grunt concat:dev

and from here you could debug any file that will be executed for the concat task. 从这里你可以调试将为concat任务执行的任何文件。 This includes dependencies of grunt-concat that would be in it's local node_modules. 这包括grunt-concat的依赖关系,它将在其本地node_modules中。 Because I found it annoying to copy and paste my executable path I made the most simple Node cli to abstract this away https://github.com/dtothefp/node-build-debug . 因为我发现复制并粘贴我的可执行文件路径很烦人所以我制作了最简单的Node cli来抽象出来这个https://github.com/dtothefp/node-build-debug If you install this globally (and of course have node-inspector installed globally) you can: 如果您全局安装(当然还有全局安装的node-inspector),您可以:

$ build-debug grunt concat:dev

In the newer Node versions (starting from 6.3 I think), there's an in-built debugger/inspector: 在较新的Node版本中(我认为从6.3开始),有一个内置的调试器/检查器:

https://nodejs.org/en/docs/inspector/ https://nodejs.org/en/docs/inspector/

Just start your Grunt app like this (on Windows): 只需像这样启动你的Grunt应用程序(在Windows上):

node --inspect %APPDATA%\npm\node_modules\grunt-cli\bin\grunt

If you install the Chrome extension Node Inspector Manager , the DevTools will open automatically. 如果您安装Chrome扩展程序节点检查器管理器 ,DevTools将自动打开。

Otherwise, you can open chrome://inspect and connect to the debugging session manually. 否则,您可以打开chrome://inspect并手动连接到调试会话。

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

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