node.js 不能从 root(或 sudo)使用 NVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21215059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use NVM from root (or sudo)
提问by Vitalii Korsakov
I've mentioned that my application uses different version of NodeJS when running from sudo.
我已经提到我的应用程序在从sudo.
$ node -v
v0.10.23
$ sudo node -v
v0.11.8-pre
This v0.11.8-precaused me some problems, so I definitely don't want to use it, but I can't change it for root.
这个v0.11.8-pre给我带来了一些问题,所以我绝对不想使用它,但我无法为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?
我尝试从 root 用户安装 nvm,但出现错误“NVM 已安装”,但从sudo. 我的问题是什么?
回答by Venkat Selvan
The below list of commands (source: digitalocean) seems to fix the problem
下面的命令列表(来源:digitalocean)似乎解决了这个问题
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.
上面的命令有点复杂,但它所做的只是将您通过 nvm 激活的任何版本的节点复制到/usr/local/目录中(用户安装的全局文件应该位于 linux VPS 上)并设置权限,以便所有用户都可以访问它们.
Hope this helps!
希望这可以帮助!
回答by SimpleJ
My solution is to create symbolic links from the versions of nodeand npmI'm using to /usr/local/bin:
我的解决办法是创建的版本的符号链接node和npm我使用到我/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 npmand nodeavailable to all users.
这使得npm与node提供给所有用户。
回答by CFrei
Your problem is, that nvmis not in the path when you use sudo.
您的问题是,nvm当您使用sudo.
So type
所以输入
$ which nvm
and the result will be something like
结果将类似于
/home/abc/mynvm/nvm
Try again now with sudo:
现在再试一次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...
我假设您随后遇到了 root 用户找不到 0.10.13 版本的问题,但让我们看看下一条错误消息...
回答by wisbucky
The fundamental reason is because nvmis not a real program. It's a bash function that gets loaded in the user's .profile, .bashrc, or ... So sudodoesn't automatically pick it up from the $PATH like most other programs.
根本原因是因为nvm不是真正的程序。它是一个 bash 函数,它被加载到用户的.profile, .bashrc, 或 ... 所以sudo不会像大多数其他程序一样从 $PATH 自动获取它。
An alternative node version manager is n: https://github.com/tj/n. That is a real program, so sudowill pick it up via the $PATH without any hacks (as long as sudohas /usr/local/binin its $PATH).
另一个节点版本管理器是n:https: //github.com/tj/n。这是一个真正的程序,所以sudo将它捡起来通过没有任何黑客的$ PATH(只要sudo有/usr/local/bin在其$ 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
回答by lzl124631x
According to README
根据README
When using nvm you do not need
sudoto globally install a module withnpm -g, so instead of doingsudo npm install -g grunt, do insteadnpm install -g grunt
使用 nvm 时,您不需要
sudo使用 全局安装模块npm -g,因此不要执行sudo npm install -g grunt,而是执行npm install -g grunt
Need sudo npm?
需要 sudo npm 吗?
In my case, I need to sudo npm run startwhich needs the access to some file requiring root access. According to this issue,
就我而言,我需要sudo npm run start访问需要 root 访问权限的某些文件。根据这个问题,
You don't use
sudo. You should instead chmod/chown the file so that the user that has nvm has access to the file;.
你不使用
sudo. 您应该改为 chmod/chown 文件,以便拥有 nvm 的用户可以访问该文件;。
In sum
总共
The maintainer of nvm strongly believe we don't need to sudo:P
nvm 的维护者坚信我们不需要sudo:P
回答by Qianyue
I had your problem too. Finally I have worked around it. Here is my solution:
我也有你的问题。最后我解决了它。这是我的解决方案:
- 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. - Install a global nvm. See this page : nvm global. As it says, "Standard nvm has known difficulties working in multi-user or rooted environments."
- 卸载 nvm 和 nodejs。以下是一些有用的链接:卸载 nvm。如果您使用 apt-get 安装了 nodejs,则可以使用命令卸载它
apt-get purge nodejs。 - 安装全局 nvm。请参阅此页面:nvm global。正如它所说,“标准 nvm 已知在多用户或 root 环境中工作有困难。”
After restarting your terminal, you can run the command sudo nvm ls.
重新启动终端后,您可以运行命令sudo nvm ls。
回答by pmontrasio
$ sudo bash -ic "nvm use stable; npm -v"
Now using node v6.3.1 (npm v3.10.3)
3.10.3
回答by Aurélien Thieriot
I have tried the same on my machine where I have nvmas well and I have a slighlty different response:
我在我的机器上也尝试过同样的方法,但我的nvm反应略有不同:
$ 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)
我的猜测是您已经在 nvm 之外安装了节点 0.11。(通过包管理器甚至从源代码)
Therefore, running node via sudo would pick up this standalone node instead.
因此,通过 sudo 运行节点将改为选择此独立节点。
Does that make sense or am I mistaken?
这是有道理的还是我错了?
回答by itchyspacesuit
The easiest solution to this will likely be to just hit the nvm.sh executable wherever it is.
对此最简单的解决方案可能是在任何地方点击 nvm.sh 可执行文件。
sudo /home/ubuntu/.nvm/nvm.sh install node
This works fine for me (assuming that's the install path).
这对我来说很好用(假设这是安装路径)。
The full install procedure would look like
完整的安装过程看起来像
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
export NVM_DIR="/home/ubuntu/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
And then you can run the command above to hit the newly installed nvm.sh
然后你可以运行上面的命令来打新安装的nvm.sh
回答by Ricky
Install nvm globally withwget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash
全局安装 nvmwget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sudo bash

