AUR安装

yay -S nvm 

安装完成以后并不是立马可以使用,具体看下面。

官方说明如下:

You need to source nvm before you can use it. Do one of the following
or similar depending on your shell (and then restart your shell):

  echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.bashrc
  echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.zshrc

You can now install node.js versions (e.g. nvm install 10) and
activate them (e.g. nvm use 10).

init-nvm.sh is a convenience script which does the following:

[ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.nvm"
source /usr/share/nvm/nvm.sh
source /usr/share/nvm/bash_completion
source /usr/share/nvm/install-nvm-exec

You may wish to customize and put these lines directly in your
.bashrc (or similar) if, for example, you would like an NVM_DIR
other than ~/.nvm or you don't want bash completion.

See the nvm readme for more information: https://github.com/creationix/nvm

因为我这里使用的是zsh,所以我执行了以下命令:

echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.zshrc
source ~/.zshrc 

这样就可以了。

使用说明

Example:
  nvm install 8.0.0                     Install a specific version number
  nvm use 8.0                           Use the latest available 8.0.x release
  nvm run 6.10.3 app.js                 Run app.js using node 6.10.3
  nvm exec 4.8.3 node app.js            Run `node app.js` with the PATH pointing to node 4.8.3
  nvm alias default 8.1.0               Set default node version on a shell
  nvm alias default node                Always default to the latest available node version on a shell

  nvm install node                      Install the latest available version
  nvm use node                          Use the latest version
  nvm install --lts                     Install the latest LTS version
  nvm use --lts                         Use the latest LTS version

  nvm set-colors cgYmW                  Set text colors to cyan, green, bold yellow, magenta, and white

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

示例

比如:我要使用12.18.1

nvm install 12.18.1

比如:我要使用16.15.0

nvm install node -v                                                                                                                                        [12:34:07]
v12.18.1

现在想换回12.18.1

nvm use 12.18.1

查看当前所使用的版本:

node -v 
v12.18.1

永久默认

使用的过程中我发现,使用以下命令后,只能临时有效。重新打开新的终端版本又变回原来的了。

nvm use 12.22.0

如果让设置永久生效呢?nvm alias default xx.xx.x

nvm use 12.22.0
nvm alias default 12.22.0

执行这两条命令就可以了。

总结

这样版本切换就非常的方便了。


版权声明:本文为lxyoucan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lxyoucan/article/details/126868907