Closed
Description
When sudoing node, 'command not found' is issued, because on most systems PATH is reset on sudo, for security reasons.
This makes 'sudo node' not work anymore.
Simply adding the following line at the end of the nvm.sh script, would fix it:
alias node='$NVM_BIN/node'
//possibly adding an alias for npm as well?
This assumes .bashrc for user contains these 3 lines:
. ~/.nvm/nvm.sh
enable aliases in sudo
alias sudo='sudo '
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
creationix commentedon Jul 1, 2011
Very neat idea. I don't want to add it to my nvm repo since it's a little invasive, but it's not hard for someone who wants to customize their environment.
I usually just source the nvm.sh file as root after running "sudo su" or modify root's .bashrc. I want more than just node when I run sudo and this way I get everything in the $NVM_BIN folder.
mpotra commentedon Jul 2, 2011
You are right, it might be a bit invasive and probably shouldn't be active in the release, but maybe just have it posted here or somewhere in future docs, so that people could find it easy.
Or maybe it might be better to have it in the script, but commented out, and people who need it, would just uncomment it - just a thought.
It took me a bit to figure this out, so probably this could save some time for others.
Modifying root's bashrc wasn't an option for my case, since we had a set-up with several users, each with its own node/nvm, and wanted to allow quick running through sudo (sudo node app_name), where necessary.
Including nvm.sh or the path/s in root's .bashrc would allow only for one single setup to run through sudo.
euskode commentedon Mar 30, 2012
This is fantastic, I made the changes and it works like a charm.
What if I wanted to do something like
sudo ENV1=val1 ENV2=val2 node server.js
?I am able to do that with other tools (I remember that forever never had an issue with it), however in my nvm-ized environment, I get
sudo: node: command not found
.Any help would be much appreciated!
zakdances commentedon Sep 20, 2012
Does "nvm install" need sudo to run error-free?
creationix commentedon Sep 20, 2012
@zakdances no, nvm installs node into a local folder inside your git clone of the nvm repo. Unless your user doesn't have write access to your own clone, then it should work fine without sudo. In fact, using sudo can break things since it creates files as the root user.
casio commentedon Sep 25, 2012
Hm, just stumbled upon this one when trying to install grunt globally.
Like @mpotra suggested, I had to also alias
npm
like so:alias node='$NVM_BIN/node'
alias npm='$NVM_BIN/npm'
So that means that not a single node package intended for global availablity can be installed when using nvm by default, right? That should make quite some users having this scenario, I imagine.
How about inventing a special nvm mode so the aliases only get activated when users explicitly enter that mode?
When they leave the mode again, the aliases could get disabled.
jcollum-hcg commentedon Jul 19, 2013
This seems common enough that it should be added to the readme.
darethas commentedon Aug 12, 2013
Yes, needing sudo on *nix systtems is very common.
As a suggestion, you could try a solution similar to what ruby version manager does http://rvm.io/integration/sudo
jcollum-hcg commentedon Aug 12, 2013
I was just told in the git mailing list that you should only very rarely use sudo. But I see it all the time. Too many opinions about it.
tjwebb commentedon Dec 14, 2013
Using
sudo
to install node is perfectly reasonable and common. Running node itself as root is frowned upon. Unfortunatelynvm
makes this very difficult.tjwebb commentedon Dec 14, 2013
If anyone is looking for an easy way to install nvm globally: https://github.com/xtuple/nvm
koenpunt commentedon Dec 15, 2013
There is no need to install node with sudo once you give the
nvm
directory the right permissions.For example:
create a user group called
nvm
, add every user that need to use nvm to this group and change the permissions of your nvm dir to group writeable:chmod 2775 /usr/local/nvm
(the first digit (2) is thesetgid
bit, explained here, which ensures the group for execution in this directory stays the same)endorama commentedon Jan 21, 2014
+1 for nvmsudo solution proposed by @treehau5
drkibitz commentedon Jan 21, 2014
+1 for @koenpunt point on group access.
36 remaining items