使用NVM设置节点版本或安装(如果不可用). [英] Set node version with NVM or install if not available.

查看:117
本文介绍了使用NVM设置节点版本或安装(如果不可用).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些东西添加到我的bash配置文件中,这会将我的节点版本设置为特定版本,如果未安装该节点版本,请安装它.到目前为止,我所拥有的是:

I'm trying to add to my bash profile something that will set my node version to a specific version, and if the node version is not installed then install it. What I have so far is:

. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm use v6.9.1) == "" ]]; then
  nvm install v6.9.1
fi

但是,问题是$(nvm use v6.9.1)在子shell中运行,并且我的节点版本没有切换.

However, the problem is that the $(nvm use v6.9.1) is run in a subshell and my node version doesn't get switched.

a)有什么方法可以使$(nvm use v6.9.1)在当前shell中运行?

a) Is there any way to have $(nvm use v6.9.1) run in the current shell?

b)有更好的方法吗?

b) Is there a better way of doing this?

以前我只是在运行nvm install v6.9.1,但这有点慢,这是一个问题,因为每次我打开新终端时它都会运行.

Previously I was just running nvm install v6.9.1 but this was kinda slow which was an issue as it runs each time I open a new terminal.

谢谢马特!

推荐答案

您是否尝试过grepping nvm ls?

Have you tried grepping nvm ls?

. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm ls | grep v6.9.1) == "" ]]; then
  nvm install v6.9.1
else
  nvm use v6.9.1
fi

它比您使用nvm install v6.9.1快吗?

您还可以设置默认情况下始终将加载的默认版本.您可以通过运行nvm alias default 6.9.1来实现.

You can also set a default version that will always be loaded by default. You can do it by running nvm alias default 6.9.1.

您可以尝试将脚本更改为此:

You can try changing your script to this:

if [[ $(node -v) != "v6.9.5" ]]; then
  nvm install v6.9.5
  nvm alias default v6.9.5
fi

这会花一点时间,但这只是第一次

It will take a little long, but just for the first time

这篇关于使用NVM设置节点版本或安装(如果不可用).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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