开始学习
NVM 版本管理工具
nvm 是 Node Version Manager 的缩写,是一个用于管理多个 node 版本的 shell 脚本工具。在项目开发中,我们可能需要用不同版本的 Node.js 开发和测试,nvm 就可以帮助我们设置默认 node 版本,在不同开发环境中切换不同版本。
GitHub 地址:https://github.com/nvm-sh/nvm
安装 nvm
nvm 项目提供了一个 install.sh 脚本帮助用户快速安装,可以使用 cURL 或 Wget 等工具下载。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
或者
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
运行上面命令中的任意一条,就会下载并运行 v0.39.1 版本的 nvm,默认安装位置为 ~/.nvm
,并会在一些配置文件中添加如下代码片段,例如 ~/.bash_profile
、~/.zshrc
、~/.profile
或 ~/.bashrc
。
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
运行完安装脚本后,可以执行 command -v nvm
命令检查 nvm 是否安装成功。如果提示没有找到 nvm 命令,则需要重新打开 shell 终端,或者执行对应 shell 的配置文件。例如:
- bash:
source ~/.bashrc
- zsh:
source ~/.zshrc
- ksh:
. ~/.profile
利用 Git 安装
除了使用 cURL 和 Wget 命令安装,我们还可以利用 Git 来安装 nvm,这样可以更好地管理 nvm 工具本身。
Git 的版本需要在 v1.7.10 以上,执行下面命令下载、安装:
# 切换到用户家目录
cd ~/
# 克隆 nvm 仓库到本地的 .nvm 目录
git clone https://github.com/nvm-sh/nvm.git .nvm
# 切换到 ~/.nvm 目录
cd ~/.nvm
# 检出当前最新的 v0.39.1 发布版本
git checkout v0.39.1
# 激活 nvm 命令
. ./nvm.sh
在 ~/.bashrc
、~/.profile
或 ~/.zshrc
中添加下面代码片段:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
这样就完成 nvm 的安装了!
后续升级的话,可以参考下面步骤:
# 切换到 $NVM_DIR 目录(即 ~/.nvm 目录)
cd "$NVM_DIR"
# 拉取远程仓库中最新的更改
git fetch --tags origin
# 检出最新版本到本地
git checkout git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)
# 激活 nvm 命令
. "$NVM_DIR/nvm.sh"
使用示例
下载、编译和安装 node 的最新版本
nvm install node # "node" 是最新版本的别名
也可以指定安装版本,例如 v10.24.1
nvm install 10.24.1
如果不知道有哪些版本,可以先列出所有可用的版本
nvm ls-remote
使用指定版本(可以不指定完整版本号)
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6
获取可执行文件的路径
nvm which 16