Running Grunt from Visual Studio post build event command line

12,374

Solution 1

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).

Solution 2

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

Solution 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.

Share:
12,374
Reedling78
Author by

Reedling78

Updated on June 28, 2022

Comments

  • Reedling78
    Reedling78 about 2 years

    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.

  • Victor
    Victor almost 11 years
    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!
  • Mike Pugh
    Mike Pugh almost 11 years
    @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.
  • Victor
    Victor almost 11 years
    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.
  • Mike Pugh
    Mike Pugh almost 11 years
    @Victor strange... it's working w/o issue for me. For what it's worth, I'm using VS 2012 Ultimate.
  • Victor
    Victor almost 11 years
    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.
  • Mike Pugh
    Mike Pugh almost 11 years
    @Victor - Grunt exit code 6 is a warning. What are you trying to have your grunt file do?
  • Victor
    Victor almost 11 years
    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?
  • Mike Pugh
    Mike Pugh almost 11 years
    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.
  • Victor
    Victor almost 11 years
    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.
  • Mike Pugh
    Mike Pugh almost 11 years
    Strange! Check out grunt-karma, the karma test runner karma-runner.github.io/0.10/index.html works quite well with Grunt.
  • Kyle Heon
    Kyle Heon over 10 years
    I had to be running Visual Studio (2012) as Administrator for it to find grunt but this worked once I did that.
  • Richard Edwards
    Richard Edwards almost 10 years
    To use 2 commands I had to specifically use {CTRL}+{ENTER} to separate the lines, just using {ENTER} key didn't work in VS2013