-bash: cordova: command not found; or -bash: phonegap: command not found

I am not going to ask a question, but answer one, as I have found this advice nowhere online thus far and have just figured this out myself. It's good to share, right?


So on the command line, I did this:

sudo npm install -g cordova
sudo npm install -g phonegap

Which worked fine, but when I then ran either of these lines:

My-MacBook-Pro:~ username$ cordova
My-MacBook-Pro:~ username$ phonegap

I got these messages:

-bash: cordova: command not found
-bash: phonegap: command not found

Why? It turned out that the permissions on my /usr/local/lib directory were set to "everyone: No Access". I changed that to "everyone: Read only" and tried again.

My-MacBook-Pro:~ username$ cordova
My-MacBook-Pro:~ username$ phonegap

This time they worked! As a test I turned it back to "everyone: No Access" to see if it really was the problem. This time I got different messages:

-bash: /usr/local/bin/cordova: Permission denied
-bash: /usr/local/bin/phonegap: Permission denied

The outcome was the same though, I could not call either cordova or phonegap through the command line (I'm including these last two denial messages just in case anyone searches on them).


I'm on Mac Os Mountain Lion. After several attempts, turns out the solution is pretty quick.

Before installing Cordova, make sure you are the owner of the folders it's going to install into. Open terminal and type:

sudo chown -R $USER /usr/local/bin
sudo chmod -R 0775 /usr/local/bin

sudo chown -R $USER /usr/local/lib/node_modules
sudo chmod -R 0775 /usr/local/lib/node_modules
npm install -g cordova

If you installed a new version of node.js, you could still get some error. Try deleting npm cache:

sudo npm cache clear
npm install -g cordova

Then type cordova and good luck ;)

EDIT: now updated to work with macOs Mojave.

Note: Mojave won't allow you to change the ownership of /usr/local/altogether, throwing an error

chown: /usr/local: Operation not permitted

The fix is pretty easy, we just need to specify the path to the node_modules subfolder.

This change will also fix similar common issues when installing many other popular packages.


Take a look at your $Path variables by running the following in the terminal:

echo "$PATH" 

If you don't see the same path cordova is trying to install, you need to fix the install location.

Run:

 npm config get prefix

The default on OS X is /usr/local, which means that npm will symlink binaries into /usr/local/bin, which should already be on your PATH. If you have a different path, configure npm to have the local directory.

So run this to change it to /usr/local:

 npm config set prefix /usr/local

That did it form me. Credit goes to: npm global path prefix


You need to add "/usr/local/share/npm/bin" directory to your $PATH variable. Open your Terminal / Command line and type:

export PATH=$PATH:/usr/local/share/npm/bin