Can't use NVM from root (or sudo)

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I've mentioned that my application uses 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've tried to install nvm from root user, but got error "NVM already installed", but still nvm not found when running from sudo. What is my problem?

回答1:

The below list of commands (source: digitalocean) 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's doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS) and setting the permissions so that all users can access them.

Hope this helps!



回答2:

My solution is to create symbolic links from the versions of node and npm I'm using to /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 npm and node available to all users.



回答3:

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

So type

$ which nvm

and the result will be something like

/home/abc/mynvm/nvm

Try again now with sudo:

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

I assume you then run into the issue that the root user can't find the 0.10.13-version, but lets see the next error message...



回答4:

I have tried the same on my machine where I have nvm as well and I have a slighlty different response:

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

My guess is that you have installed node 0.11 outside of nvm. (Via package manager or even from source)

Therefore, running node via sudo would pick up this standalone node instead.

Does that make sense or am I mistaken?



回答5:

I had your problem too. Finally I have worked around it. Here is my solution:

  1. Uninstall nvm and nodejs. Here are some helpful links: Uninstallation of nvm. If you installed nodejs using apt-get, you can uninstall it with the command apt-get purge nodejs.
  2. Install a global nvm. See this page : nvm global. As it says, "Standard nvm has known difficulties working in multi-user or rooted environments."

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



回答6:

The fundamental reason is because nvm is not a real program. It's a bash function that gets loaded in the user's .profile, .bashrc, or ... So sudo doesn't automatically pick it up from the $PATH like most other programs.

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

sudo npm install -g n  # 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


回答7:

$ sudo bash -ic "nvm use stable; npm -v" Now using node v6.3.1 (npm v3.10.3) 3.10.3


回答8:

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



转载请标明出处:Can't use NVM from root (or sudo)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!