问题描述
NVM,Node Version Manager,用于在系统中安装并管理多个版本 Node.js 环境,并能够自如切换。
该笔记将记录:在 Linux 中,如何安装 NVM 工具,以及管理安装并管理多个版本 Node.js 环境的方法。
解决方案
第一步、安装 nvm 工具(Linux)
Debian GNU/Linux 10 (buster) and Bash:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash cat >> ~/.bashrc <<EOF 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 EOF
第二步、使用 nvm 安装 Node.js 环境
查看可用 Node.js 的全部版本:
# nvm ls-remote ... // 查看所有可用的 13 版本 # nvm list-remote v13.10 v13.10.0 v13.10.1 ...
安装 Node.js 环境:
# nvm install v13.10.1 # 安装 v13.10.1 版本 Downloading and installing node v13.10.1... Downloading https://nodejs.org/dist/v13.10.1/node-v13.10.1-linux-x64.tar.xz... ################################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v13.10.1 (npm v6.13.7) # nvm install node # 安装 latest 版本 Downloading and installing node v17.7.2... Downloading https://nodejs.org/dist/v17.7.2/node-v17.7.2-linux-x64.tar.xz... ################################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v17.7.2 (npm v8.5.2) Creating default alias: default -> node (-> v17.7.2)
切换到特定 Node.js 版本:
nvm use v13.10.1 # 使用 v13.10.1 版本 nvm use system # 使用系统版本(通过 APT 或 YUM 而非 NVM 安装的 Node.js 版本) nvm alias default system # 设置默认版本(命令 nvm use 只设置当前终端)
常用其他命令
显示当前正在使用 node 命令的路径
# nvm which $(nvm current) /usr/bin/node
卸载 npm 工具:
npm uninstall npm -g rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
参考文献
node.js – How can the default node version be set using NVM? – Stack Overflow
nvm-sh/nvm: Node Version Manager – POSIX-compliant bash script to manage multiple active node.js versions
GitHub – nvm-sh/nvm: Node Version Manager – POSIX-compliant bash script to manage multiple active node.js
node.js – Where is node while using nvm? – Stack Overflow
How to list all available node.js versions? · Issue #84 · nvm-sh/nvm · GitHub
npm – How do I modify the configuration of Yarn? – Stack Overflow
removal | npm Docs