chrome下的Grunt插件断点调试——基于node-inspector

之前调试grunt插件时,都是通过人肉打log来调试。不仅效率低,而且会产生一堆无用的代码。于是简单google了下node断点调试的方法,总结了下。

借助node-inspector,我们可以通过Chrome浏览器的调试工具来调试。

一、安装node-inspector

npm install -g node-inspector

 

二、监听Nodejs的debug调试端口

node-inspector &

见到输出下面log,http://127.0.0.1:8080/debug?port=5858 很关键,后面我们会用到

[1] 2123
casperchenMacBookPro:demo casperchen$ Node Inspector v0.6.1
   info  - socket.io started
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

三、调试grunt任务

输入如下命令,将task替换成你要运行的命令

node --debug-brk $(which grunt) task

得到如下输出,说明grunt已经进入调试模式

debugger listening on port 5858

将上一步的http://127.0.0.1:8080/debug?port=5858帖到地址栏,回车,如果你看到下面场景,恭喜~

Alt text

 

补充:2014.02.12

三、windows下调试 

之前写这篇博文是在mac上做的调试,当回到公司日常开发用的机器上就傻眼了,因为用的是windows系统。瞎倒腾了一下,改成下面命令也勉强可用

"c:\Program Files\nodejs\node.exe" --debug-brk C:\Users\casperchen\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt task

简单介绍下上面的命令,由于对dos命令是睁眼瞎,求路过的兄弟优化~

"c:\Program Files\nodejs\node.exe" # node的安装路径

--debug-brk # 表示要进入调试模式,且在第一行断点

C:\Users\casperchen\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt # grunt的安装路径

task # grunt路径

 

参考资料:

http://www.nodejs.org/api/debugger.html

http://linmomo02.iteye.com/blog/1496736

http://stackoverflow.com/questions/11171013/using-node-inspector-with-grunt-tasks#comment16474798_11171013

posted @ 2013-12-11 12:08  程序猿小卡  阅读(4884)  评论(3编辑  收藏  举报