有人使用过node-inspector和Grunt进行应用程序调试吗?如果没有,你能推荐一个基于Grunt的应用程序调试工具吗?
有人使用过node-inspector和Grunt进行应用程序调试吗?如果没有,你能推荐一个基于Grunt的应用程序调试工具吗?
要在调试模式下运行grunt,您需要显式地将grunt脚本传递给node:
node-debug $(which grunt) task
在你的任务中添加debugger;
代码行。 node-inspector
将打开带有调试工具的浏览器。
node-inspector
添加了命令 node-debug
,它会以 --debug
状态启动 node 并在浏览器中打开 node-inspector
页面,在第一个 debugger
行或断点处停止。
在 Windows 上,情况会稍微复杂一些。请参阅 @e.gluhotorenko 的答案以获取说明。
$(which grunt)
是什么吗?它只是我想要调试的Gruntfile.js吗?在task
中,我需要输入(以serve
为例)grunt serve
还是仅仅是serve
?我尝试过node-debug Gruntfile.js serve
和很多其他方法,但我就是无法让它工作。Node-inspector打开并且在第一行处停了下来,但即使在函数的第一行放置了一个debugger;
行,我也不能进入serve
任务。 - Brettgrunt.js
脚本的完整路径所替换。这个脚本实际上运行了Grunt。如果你用的是Windows,请参考https://dev59.com/PWgu5IYBdhLWcg3wpYrJ#13443026(下面的答案)可能是grunt-cli脚本的位置。它将取代你命令中的`grunt` - 在你的例子中,将task
替换为serve
。 - David Souther运行
node --debug-brk c:\Users\username\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt taskname
在带有Gruntfile.js
的目录中使用命令行。不要忘记在必要的地方放置debugger;
代码行。
grunt-cli
包的一部分:node --debug-brk [..]\node_modules\grunt-cli\bin\grunt
。 - mistaeckonode --debug-brk (Resolve-Path ~\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt) taskname
。 - George Mauernode-inspector
。 - valter.santos.matos#!/usr/bin/env node
改为
#!/usr/bin/env node --debug-brk
--debug-brk会在运行的第一行JavaScript上中断。node --debug-brk $(which grunt) node-inspector build test
这样的东西?我喜欢。 - David Southernode-inspector
不会退出并允许 build
和 test
运行,因为它会永远运行。我添加了一个链接到一个片段,其中我已经设置好了它。 - ChrisWrennode-inspector&
,将其留在后台,并调用它完成。你对此有什么想法吗? - David Southergrunt node-inspector
,然后转到本地主机URL,但我看不到任何源文件。只有空的调试工具。 - khollenbeckgrunt node-inspector
和node --debug-brk Gruntfile.js
。然而,当我在grunt文件中设置断点时,它会崩溃并显示一个连接错误。 - khollenbeck我已经完成了一个任务,运行我的应用并启动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');
});
这里有很好的答案。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上运行它。
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
}
]
}