在 Linux 系统中,可以使用多种工具来管理 Node.js 的不同版本。以下是一些流行的 Node.js 版本管理工具及其使用方法:
nvm (Node Version Manager)简介:nvm 是最常用的 Node.js 版本管理工具,适用于 macOS、Linux 和 Windows(通过 nvm-windows 分支)。它提供了一套完整的命令行工具来安装、切换和管理多个 Node.js 版本。
安装步骤:
- 安装 nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
或者wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
安装完成后,关闭并重新打开终端窗口,或者运行以下命令使 nvm 立即生效:source ~/.bashrc
确认 nvm 是否成功安装:nvm --version
常用命令:
列出可用版本:nvm list available
安装指定版本:nvm install <version>
切换版本:nvm use <version>
设置默认版本:nvm alias default <version>
卸载版本:nvm uninstall <version>
查看当前使用的版本:nvm current
n简介:n 是一个专注于提供快速、简洁体验的 Node.js 版本管理工具,没有子shell、配置文件或复杂的 API。
安装步骤:
- 使用 npm 安装 n:
npm install -g n
安装完成后,可以通过以下命令查看 n 的版本:n --version
常用命令:
列出可升级版本:n ls
安装指定版本:n <version> 或 n latest
切换版本:n use <version> 或 n latest
显示当前使用的版本:n current
nodenv简介:nodenv 是一个类似于 Ruby 的 rbenv 的 Node.js 版本管理工具,通过修改 PATH 环境变量来切换 Node.js 版本。
安装步骤:
- 克隆 nodenv 仓库并添加到 PATH:
git clone https://github.com/nodenv/nodenv.git ~/.nodenvecho 'export PATH="$HOME/.nodenv/bin:$PATH"' >> ~/.bash_profileexec $SHELL
安装 node-build 插件以支持版本安装:git clone https://github.com/nodenv/node-build.git $(nodenv root)/plugins/node-build
常用命令:
安装版本:nodenv install <version>
切换版本:nodenv global <version> 或 nodenv local <version>
查看已安装版本:nodenv versions
asdf简介:asdf 是一个插件扩展的 Node.js 版本管理工具,支持全语言版本管理。
安装步骤:
- 安装 asdf:
curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/asdf-vm/asdf/master/install.sh | bash
将以下内容添加到你的 shell 配置文件(如 ~/.bashrc
或 ~/.zshrc
):. "$HOME/.asdf/asdf.sh". "$HOME/.asdf/completions/asdf.bash"
重新加载配置文件:source ~/.bashrc
常用命令:
安装 Node.js 版本:asdf install nodejs <version>
使用 Node.js 版本:asdf local nodejs <version>
列出已安装的 Node.js 版本:asdf list nodejs
通过这些工具,你可以在 Linux 系统上轻松管理 Node.js 的不同版本,以满足不同项目的需求。