Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic, npm and cordova commands all require sudo to work

The sudo, by the looks of what I have seen in some forums, is why I am getting an error when running sudo ionic emulate ios in this post New to ionic - can’t build for ios (9) on El Capitan, and sudo ionic is required, deprecated npmconf. (I am running El Capitan btw).

Whenever I need to use an ionic, npm, or cordova command, I always have to put sudo in front of it otherwise I get a "bash command not found" error. I have tried to use this fix https://www.npmjs.com/package/npm-sudo-fix but it doesn't work. This is what happens in the log:

Dylans-MBP:Ionic Projects Dylan $ sudo npm install -g npm-sudo-fix
Password:
/usr/local/bin/npm-sudo-fix -> /usr/local/lib/node_modules/npm-sudo-fix/index.js
[email protected] /usr/local/lib/node_modules/npm-sudo-fix
└── [email protected] ([email protected])
Dylans-MBP:Ionic Projects Dylan$ sudo npm-sudo-fix
chown: /users/root/.npm: No such file or directory
like image 852
Dylanthepiguy Avatar asked Jan 19 '16 07:01

Dylanthepiguy


1 Answers

Edit (28 August 2018):

I wrote this post and answer many years ago when I was not very familiar with the terminal. Now that I am more experienced, I can recommend the better solution. It is good practice to avoid unnecessary usages of sudo.

I believe the problem is caused by installing node JS from the installer from the node JS website. This version of node JS seems to write various files in the filesystem as root, potentially (and unnecessarily) causing global packages to require root permissions to be installed.

The ideal solution would be to completely remove node JS, that was installed from the node JS installer from the website, from your machine (see https://stackabuse.com/how-to-uninstall-node-js-from-mac-osx/). Then install node from a commandline package manager, such as homebrew.


Original Answer (2016)

Finally came up with a solution by playing with the permissions! Hopefully I didn't stuff up anything. (I am a terminal noob btw.) Anyway, here's the solution.

Run this code here in the terminal (you may or may not have to run cd .. before hand)

sudo chown -R $USER /usr/local/

This changes the permissions of every thing inside the local folder (hence the -R which means recursive). (I found this line somewhere in the ionic forums).

Now, you have to reinstall cordova and ionic

sudo npm install -g cordova
sudo npm install -g ionic

For me anyway, after I do this, I can type ionic and not get any errors. I get this in the terminal if I run cordova though.

/usr/local/lib/node_modules/cordova/node_modules/update-notifier/node_modules/configstore/index.js:53
                throw err;
                ^

Error: EACCES: permission denied, open '/Users/Dylan/.config/configstore/update-notifier-cordova.json'
You don't have access to this file.

What I did to fix this was do this (after opening up a new terminal window):

sudo chown -R $USER Dylan

(Dylan is my user folder; replace it with yours. Also, you may have to do a cd .. before running that line And that fixes the permissions for that config file. Now running cordova works without errors! Woot! That error might come back though, so you have to run that line of code again.

Hope this helps somebody!

like image 85
Dylanthepiguy Avatar answered Oct 21 '22 04:10

Dylanthepiguy