Set node version with NVM or install if not available.

偶尔善良 提交于 2019-12-01 10:55:36

问题


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

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) Is there any way to have $(nvm use v6.9.1) run in the current shell?

b) Is there a better way of doing this?

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.

Thanks Matt!


回答1:


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

Is it any faster than using nvm install v6.9.1 for you?

EDIT: 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



来源:https://stackoverflow.com/questions/49436894/set-node-version-with-nvm-or-install-if-not-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!