47

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

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
richtextformat
  • 471
  • 1
  • 4
  • 4

20 Answers20

72

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.

Francesco Frapporti
  • 5,195
  • 4
  • 32
  • 39
  • @DeborahSpeece can you be more specific? Maybe you are installing the files in another location or the problem is different. What version of OSX are you using? – Francesco Frapporti May 08 '14 at 11:17
  • Using Mac OSX Mavericks. After a lot of hair ripping, npm installed both cordova and phonegap for local, not global user, even with -g and -gf. I was not able to get either installed for global user. – Deborah May 09 '14 at 03:28
  • 1
    @richtextformat why this answer is not yet accepted? – Lithu T.V Jul 15 '14 at 07:02
17

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

Community
  • 1
  • 1
MethodMan
  • 409
  • 5
  • 8
11

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
itamaryu
  • 326
  • 3
  • 5
  • I recommend also adding the path permanently: http://blog.just2us.com/2011/05/setting-path-variable-in-mac-permanently/ – gardenofwine Mar 25 '14 at 08:24
  • It also helped me with error 'Error: ios-sim was not found. Please download, build and install version 1.7 or greater from https://github.com/phonegap/ios-sim into your path. Or 'npm install -g ios-sim' using node.js: http://nodejs.org/' – Rostyslav Druzhchenko Aug 18 '14 at 07:33
10

This happened to me too. Fortunately found this great write up: http://blog.webbb.be/command-not-found-node-npm/

I'll summarize it here. This can happen when npm is installing to a location that is not the standard and is not in your path.

To check where npm is installing, run: npm root -g

It SHOULD say /usr/local/lib/node_modules

Mine said /Users/myusername/.npm-packages/lib/node_modules

To set it to the correct location:

  • run: npm config set prefix /usr/local

  • Then reinstall your npm package(s) with -g: npm install -g cordova etc

Ira Herman
  • 2,796
  • 1
  • 23
  • 22
5

For Ubuntu users, it is located at ~/.npm-global/bin

Adding the following line in ~/.bashrc is gonna fix the problem.

export PATH=${PATH}:~/.npm-global/bin
takasoft
  • 1,228
  • 15
  • 15
2

I have to call the phonegap command by using nodejs like this...

nodejs /usr/local/lib/node_modules/phonegap/bin/phonegap.js create my-app

When I run the cordova command directly using

/usr/local/lib/node_modules/cordova/bin/cordova

it returns nothing

Natus Drew
  • 1,876
  • 1
  • 21
  • 23
2

In OSX:

After installing npm with brew, if you still get command not found, I suggest you check your hidden ~/.npm-packages/bin directory, and see if you see 'cordova' inside. You can do this in the following way:

ls ~/.npm-packages/bin | grep cordova

If you see cordova there, you just need to make sure this directory is in your path. If you're using bash, then make sure to edit your ~/.bashrc with the following (alternative, if you're using zsh, edit the following file ~/.zshrc) by adding this line at the end (replacing EDIT_THIS_PUT_YOUR_USER_NAME_HERE with your actual user name):

export PATH=$PATH:/Users/EDIT_THIS_PUT_YOUR_USER_NAME_HERE/.npm-packages/bin

Afterwards either source the file you just edited or simply exit the terminal and open a new one. To source the file here are the instructions: If you edited your ~/.bashrc then just type 'source ~/.bashrc', alternatively if you're using zsh instead of bash, and you edited ~/.zshrc, then simply type 'source ~/.zshrc' and you're good to go. You should now be able to type 'cordova' and see the correct output in the screen.

Andres
  • 485
  • 1
  • 4
  • 10
2

On Mac OS:

  • Fix permission:

    sudo chown -R <USER LOGIN>:wheel /usr/local/*

  • Install cordova:

    npm install -g cordova

    path cordova installed: /usr/local/Cellar/node/11.10.1/bin/cordova

  • Type:

    cordova

    if message show:

    -bash: cordova: command not found

  • Map cordova to usr/bin:

    sudo ln -s /usr/local/Cellar/node/11.10.1/bin/cordova /usr/bin/

  • Re-type:

    cordova

    Bash cordova worked

Vinh Lee
  • 101
  • 1
  • 3
1

I just asked the same question with NPM and recently got an answer(npm command not found after updating to Mac OSX 10.9.4), you could also apply it here like i did. Try this:

sudo -s ln -s /usr/local/lib/node_modules/phonegap/bin/phonegap.js /bin/usr/cordova

It worked for me.

Community
  • 1
  • 1
clintgh
  • 2,039
  • 3
  • 28
  • 44
1

All didnt work for me untill:

sudo rm -R /usr/local/lib/node_modules/cordova
npm install -g cordova
Jacek Pietal
  • 1,980
  • 1
  • 18
  • 27
1

You can run cordova without package setup easily. Just add npx in front of any command you want to run. Ex:

npx cordova create myApp org.apache.cordova.myApp myApp
npx cordova platform add browser
user1079877
  • 9,008
  • 4
  • 43
  • 54
1

Anyone looking for a 2021 answer look no further than @Method Man's solution as follows :

"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 "

MethodMan
  • 409
  • 5
  • 8
0

You've to add the path to the cordova bin in the bash file.

/etc/profile
~/.bash_profile
~/.bash_login   (if .bash_profile does not exist)
~/.profile      (if .bash_login does not exist)

Try to close and reopen the teminal after edited the file.

Lwyrn
  • 1,821
  • 1
  • 16
  • 27
0

The binary files after being installed by npm will be located in /usr/local/share/npm/bin. Feel free to add that path to the PATH of your system in order to use them

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
0

You should update your environmental variables paths, if phonegap is installed then you should find it in:

/usr/local/lib/node_modules/phonegap/bin/

if you found this path then add it to your paths.

Hazem Hagrass
  • 9,329
  • 10
  • 32
  • 54
0

For me, when installing node on a OSX Yosemiti with Homebrew, the Cordova package wasn't being created in the appropriate /usr/local/lib location. I had to explicitly copy the cordova folder from my .npm-packages to where it should have been installed with homebrew.

The following command worked for me:

sudo cp ~/.npm-packages/lib/node_modules/cordova /usr/local/lib/node_modules/cordova
SBerg413
  • 14,515
  • 6
  • 62
  • 88
0

In my case the problem was, npm prefix was set to a different path and cmd was not able to pick from it.

I resolved the issue using

npm config set prefix "/usr/local"

Before trying this you could see teh actual prefix being used by npm using the following command,

npm config get prefix 
ChandrasekarG
  • 1,384
  • 13
  • 24
0

This is very basic, but for me the problem was trying to install cordova in the project folder, rather than the local folder. Easy fix.

WhooNo
  • 911
  • 2
  • 12
  • 28
0

I encountered this problem while following a tutorial that instructed me to install cordova-android:

npm i cordova-android

But nowhere did it say you should first install cordova CLI globally!

npm i -g cordova

After installing cordova CLI the problem was solved.

Shaya
  • 2,792
  • 3
  • 26
  • 35
0

Happened in 2022. Response was cordova: command not found

In my case, it was multiple node versions. Some new node versions (I have installed Cordova with an older version) did not identify the cordova command.

The solution was simple, switch to the node version which you installed Cordova with

Arosha
  • 1,311
  • 2
  • 16
  • 22