Solving Grunt “Command Not Found” Error in Terminal [Quickfix]

In one of our previous post we have discussed how to build customized jQuery. In the process, we used Grunt to perform the task, for testing, compiling, and compressing the library. However, some of you might encounter an issue where the command grunt is not recognized in your system, even when we had run npm install -g grunt-cli to install the package.

In this case, you typically will get the following error message in your Terminal.

 -bash: grunt: command not found 

The “Command Not Found” error occur is due to the Grunt binary not having been added to the shell – this most likely happened since Grunt 0.4. Here is how to get it fixed.

Step 1

First, let’s check whether the .bash_profile exists.

 test -e ~/.bash_profile && echo "Found" || echo "Not Found" 

If it exists, it will return Found, as shown in the following screenshot.

The .bash_profile is a shell script that is executed when we start the Terminal. To be able to run the grunt command we need to link the path where the Grunt binary is located in this file. In the case where it returns Not Found, meaning that it doesn’t exist, you need to create it first by running this command line.

 touch ~/.bash_profile 

Run the test again to ensure.

Step 2

Open the .bash_profile in TextEdit using this command line, so that we are able to edit the content easily.

 open -a TextEdit ~/.bash_profile 

When we are installing Grunt, we will see the path where the binary is added from the report.

Add the path in .bash_profile, as follows.

 export PATH=/usr/local/share/npm/lib/node_modules/grunt-cli/bin:$PATH 

Save it and exit.

Step 3

To ensure that our .bash_profile is executed when we open the Terminal, run this command line.

 source ~/.bash_profile 

Restart your Terminal, run a test whether the grunt command is now recognized:

 grunt --version 

If it works, you should now see the following message.