如何执行“sudo nvm"? [英] How to execute "sudo nvm"?

查看:124
本文介绍了如何执行“sudo nvm"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Mac 上,我想将一些需要 su 权限的包迁移到另一个节点版本.

On my Mac, I want to migrate some packages that require su rights to another node version.

我使用自制软件安装 nvm,现在我需要执行 sudo nvm 或 --reinstall-packages 将失败.

I used homebrew to install nvm and now I need to execute sudo nvm or --reinstall-packages will fail.

me@MacBook:~$ sudo nvm
sudo: nvm: command not found
me@MacBook:~$ sudo node -v
v5.4.1
me@MacBook:~$ sudo npm -v
3.3.12
me@MacBook:~$ nvm ls
->       v5.4.1
         v9.6.1
         system
default -> 5.4.1 (-> v5.4.1)
node -> stable (-> v9.6.1) (default)
stable -> 9.6 (-> v9.6.1) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.8.7 (-> N/A)
lts/boron -> v6.13.0 (-> N/A)
lts/carbon -> v8.9.4 (-> N/A)

我认为找不到该命令,因为 sudo 正在寻找不同的路径.我在 /usr/local/opt/nvm 中找到了 nvm.sh,但是:

I think the command cannot be found, because sudo is looking in different paths. I've found nvm.sh in /usr/local/opt/nvm, however:

sudo /usr/local/opt/nvm/nvm.sh ls

什么都不返回.

偶数

/usr/local/opt/nvm/nvm.sh ls

什么都不返回,所以我怀疑这是错误的 shell 脚本.

returns nothing, so I suspect this is the wrong shell script.

如何使用 sudo 显式调用 nvm?

How can I call nvm with sudo, explicitly?

推荐答案

考虑定义一个 shell 函数包装器:

Consider defining a shell function wrapper:

nvmsudo() { sudo bash -lic '. /usr/local/opt/nvm/nvm.sh && nvm "$@"' _ "$@"; }

...之后:

nvmsudo --version

...或...

nvmsudo install 5.4.1 --reinstall-packages-from=9.6.1

<小时>

解释上面的逻辑:


To explain the above logic:

  • 使用 -l-i 参数来 bash 确保目标用户(在本例中为 root)的点文件运行,这似乎是 nvm 正确操作所必需的.
  • bash -c 的下一个参数必须是一个脚本.在执行脚本的上下文中,后面的参数变为 $0$1 等.
  • "$@" 在函数上下文中是指函数的完整参数集.
  • "$@"bash -c 脚本的上下文中是指完整的参数集(从 $1 开始,因此跳过_) 在 -c 之后传递给 shell.
  • Using the -l and -i arguments to bash ensure that dotfiles for the target user (in this case root) are run, which appears to be necessary for nvm's correct operation.
  • bash -c's immediate next argument must be a script. The arguments following that become $0, $1, etc. in the context where the script is executed.
  • "$@" in the function context refers to the full set of arguments to the function.
  • "$@" in the bash -c script's context refers to the full set of arguments (starting at $1, so skipping the _) passed to the shell after the -c.

这篇关于如何执行“sudo nvm"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆