Unable to use NVM as root (or sudo)

I mentioned that my application uses a different version of NodeJS when running from sudo .

 $ node -v v0.10.23 $ sudo node -v v0.11.8-pre 

This v0.11.8-pre caused me some problems, so I definitely don't want to use it, but I can't change it for root.

 $ sudo nvm use v0.10.23 sudo: nvm: command not found 

I tried to install nvm as root, but got the error "NVM is already installed", but nvm was not found when working with sudo . What is my problem?

+44
Jan 19 '14 at 9:24
source share
8 answers

The following is a list of commands (source: digitalocean ) that seems to fix the problem

 n=$(which node); \ n=${n%/bin/node}; \ chmod -R 755 $n/bin/*; \ sudo cp -r $n/{bin,lib,share} /usr/local 

The above command is a bit complicated, but all it does is copy any version of node that you activated with nvm to the /usr/local/ directory (where the user installed the global files, must live on VPS Linux) and set permissions so that everything users could access them.

Hope this helps!

+103
Apr 27 '15 at 18:46
source share

My solution is to create symlinks from the node and npm versions that I use for /usr/local/bin :

 sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node" sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm" 

This makes available to all npm and node users.

+33
Oct 17 '16 at 5:10
source share

Your problem is that nvm not in the way when you use sudo .

So type

 $ which nvm 

and the result will look like

 /home/abc/mynvm/nvm 

Try again with sudo :

 sudo /home/abc/mynvm/nvm use v0.10.23 

I assume that you are faced with a problem that the root user cannot find the 0.10.13 version, but allows you to see the following error message ...

+11
Jan 29 '14 at 6:26
source share

I tried the same thing on my machine where I have nvm and I have a great answer:

 $ sudo node --version sudo: node: command not found 

I assume that you installed node 0.11 outside of nvm. (Via the package manager or even from the source)

Therefore, running node through sudo will replace this standalone node instead.

Does this make sense or am I mistaken?

+4
Jan 19 '14 at 12:10
source share

I also had your problem. Finally, I worked on it. Here is my solution:

  • Remove nvm and nodejs. Here are some useful links: Removing nvm . If you installed nodejs using apt-get, you can remove it using the apt-get purge nodejs .
  • Install global nvm. See this page: nvm global . As they say, "Standard nvm knows the difficulties in a multi-user or root environment."

After restarting the terminal, you can run the sudo nvm ls .

+2
Feb 23 '15 at 21:16
source share

The main reason is that nvm not a real program. This is a bash function that is loaded by the user .profile , .bashrc or ... Thus, sudo does not automatically select it from $ PATH, like most other programs.

Alternative node version manager n : https://github.com/tj/n . This is a real program, so sudo will select it via $ PATH without any hacks (as long as sudo has /usr/local/bin in its $ PATH).

 sudo npm install -gn # install 'n' globally which n # should be /usr/local/bin/n sudo n lts # need sudo to switch node versions node --version # v6.10.0 sudo node --version # v6.10.0 
+2
Mar 02 '17 at 1:17
source share

Install nvm globally using wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash

+1
Apr 29 '16 at 0:21
source share
 $ sudo bash -ic "nvm use stable; npm -v" Now using node v6.3.1 (npm v3.10.3) 3.10.3 
+1
Oct 08 '16 at 7:57
source share



All Articles