Using node-inspector with Grunt tasks Using node-inspector with Grunt tasks node.js node.js

Using node-inspector with Grunt tasks


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

node-debug $(which grunt) task

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

Edit 28 Feb 2014

node-inspector has added the command node-debug, which launches node in a --debug state and opens the browser to the node-inspector page, stopping when it hits the first debugger line or set breakpoint.

Edit 30 January 2015

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


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.


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/binI opened the file and modified:

#!/usr/bin/env node

To

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

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

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

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