15

I've attempted to do this both in Visual Studio 2010 and Visual Studio 2012. If my Gruntfile.js file is in the root of my project I can run the "grunt" command from the post build event command line and it runs without a problem.

grunt or grunt.cmd

But if it's in a sub directory

$(ProjectDir)Public\grunt or $(ProjectDir)Public\grunt.cmd

It gives me this error

The command "c:\web\Public\grunt.cmd" exited with code 9009.

I've been researching this but I'm not finding any much help out there. I did find in the grunt documentation that I need to use "grunt.cmd" instead of just calling "grunt" but thats not helping me much.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Reedling78
  • 413
  • 1
  • 4
  • 10
  • 4
    Quick note for anyone finding this via google - I just ran into this issue and my path was setup correctly, but I had done so after Visual Studio was opened meaning the path updates were not available in Visual Studio. Closing all Visual Studio windows then re-opening the solution actually fixed my issue (just like you might do with a console window - could also re-source it too but that doesn't make sense for Visual Studio). – longda Jan 30 '14 at 22:42
  • 1
    *doh* this just helped me, thanks @longda. .. close and reopen .."have you tried turning it off and on?" .. – peter Jun 26 '15 at 10:08

4 Answers4

13

What's happening is you're specifying an exact path for grunt, which doesn't actually reside at $(ProjectDir)Public\. When you're in that directory on a command prompt and type grunt, it executes because you've set your path environment variable to include the directory where grunt lives.

Luckily the post build commands in VS act like a command window, so you can put this in your post build commands:

CD $(ProjectDir)Public\
grunt

And that should work (assuming a default grunt task is defined).

Mike Pugh
  • 6,787
  • 2
  • 27
  • 25
  • I get the following when I try the above. "The command "CD C:\MyDocs\Code\GruntTest\GruntTest\ grunt" exited with code 6. What am I missing? Has this solution worked for anyone else? Thanks! – Victor Sep 19 '13 at 15:41
  • @Victor make sure the CD and the grunt command are on separate lines. From your message it appears they're being executed at the same time. I've used the solution I posted without any issues. One way to verify is go into a command prompt and CD to that directory, and then run the grunt command manually. If that works, it should work via a build event. – Mike Pugh Sep 20 '13 at 16:47
  • 1
    I do have the commands on separate lines (stackoverflow doesn't allow for multi-line comments). Also, if I go into the command prompt and CD to the project directory it does work. It still looks as thought it's trying to execute it as one command though. – Victor Sep 20 '13 at 17:12
  • @Victor strange... it's working w/o issue for me. For what it's worth, I'm using VS 2012 Ultimate. – Mike Pugh Sep 20 '13 at 18:39
  • It's very strange indeed. I'm on VS 2012 Professional. I'm going to try on a new project from scratch and see if I can shake something loose. Do you have the grunt-cli installed? I've tried pointing to this as well, to no avail. – Victor Sep 20 '13 at 19:35
  • @Victor - Grunt exit code 6 is a warning. What are you trying to have your grunt file do? – Mike Pugh Sep 21 '13 at 19:19
  • Right now I'm just trying to get QUnit to run and output the tests in VS. Should I try another task just to see if this is working? – Victor Sep 23 '13 at 15:30
  • Well, if you can go to the directory in a command prompt and type "grunt" (or "grunt test", whatever you've got it set up as) and it runs successfully w/o any warnings then I would think it'd work. You can certainly try a simpler task that only has 1 task running (ie, setup a grunt-contrib-clean task or something), vs a multi-step process. – Mike Pugh Sep 24 '13 at 16:59
  • You're right on Mike...if I try running the grunt-contrib-clean task it works just fine. So, this appears to be something specific to running the qunit task. – Victor Sep 30 '13 at 16:21
  • Strange! Check out grunt-karma, the karma test runner http://karma-runner.github.io/0.10/index.html works quite well with Grunt. – Mike Pugh Oct 01 '13 at 17:38
  • I had to be running Visual Studio (2012) as Administrator for it to find grunt but this worked once I did that. – Kyle Heon Dec 18 '13 at 18:51
  • To use 2 commands I had to specifically use {CTRL}+{ENTER} to separate the lines, just using {ENTER} key didn't work in VS2013 – Richard Edwards Jul 31 '14 at 16:35
3

If you had Visual Studio open and then:

  1. Installed node package manager (npm) and grunt
  2. Then tried to run pre/post build commands including grunt command.

The build will simply fail with the "exited with code 9009" message. (Meaning "I don't know what grunt command is")

To resolve this situation just close visual studio and reopen it (as @longda mentioned on his comment) and everything will work just fine.

I'm using VS 2013 Premium and latest version of npm/grunt.

Miguel
  • 1,575
  • 1
  • 27
  • 31
3

You can run as post build using task runner as shown below. Right click on the build --> bindings--> and then specify if you need it to run post or pre build

Community
  • 1
  • 1
Prathap Kudupu
  • 1,315
  • 11
  • 11
0

Somewhat related, I had a weird issue that xcopy would not run after grunt, running them as a single command fixed it:

cd $(SolutionDir)..\App
grunt release && xcopy "$(SolutionDir)..\App\release" "$(TargetDir)Content\" /Y /E /S
jgillich
  • 71,459
  • 6
  • 57
  • 85