返回
NVM使用记录:巧用NVM管理Node.js版本,轻松切换开发环境
前端
2023-10-19 05:37:45
NVM 简介
Node.js 版本管理工具 (NVM) 是一个用于管理 Node.js 版本的工具。它允许您在不同的项目中使用不同的 Node.js 版本,并轻松地在版本之间切换。NVM 可以安装在 Windows、macOS 和 Linux 系统上。
NVM 安装
Windows
- 下载 NVM 安装程序:https://github.com/nvm-sh/nvm/releases
- 双击安装程序并按照提示进行安装
- 在环境变量中添加 NVM 的路径:
- 打开控制面板
- 点击“系统和安全”
- 点击“系统”
- 点击“高级系统设置”
- 点击“环境变量”
- 在“用户变量”下,找到“Path”变量,并点击“编辑”
- 在“变量值”中,添加
;C:\Program Files\nvm
(假设您将 NVM 安装在 C 盘的 Program Files 文件夹下) - 点击“确定”保存更改
macOS
- 使用 Homebrew 安装 NVM:
brew install nvm
- 在
.zshrc
或.bash_profile
文件中添加以下内容:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
- 重新加载配置文件:
source ~/.zshrc
或source ~/.bash_profile
Linux
- 使用包管理器安装 NVM:
- Ubuntu/Debian:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- CentOS/Fedora:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Ubuntu/Debian:
- 在
.zshrc
或.bash_profile
文件中添加以下内容:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
- 重新加载配置文件:
source ~/.zshrc
或source ~/.bash_profile
NVM 使用
列出已安装的 Node.js 版本
nvm ls
安装 Node.js 版本
nvm install <version>
例如,要安装 Node.js 16.14.2 版本,可以使用以下命令:
nvm install 16.14.2
切换 Node.js 版本
nvm use <version>
例如,要切换到 Node.js 16.14.2 版本,可以使用以下命令:
nvm use 16.14.2
卸载 Node.js 版本
nvm uninstall <version>
例如,要卸载 Node.js 16.14.2 版本,可以使用以下命令:
nvm uninstall 16.14.2
NVM 常用命令
命令 | |
---|---|
nvm ls | 列出已安装的 Node.js 版本 |
nvm install |
安装 Node.js 版本 |
nvm use |
切换 Node.js 版本 |
nvm uninstall |
卸载 Node.js 版本 |
nvm current | 显示当前使用的 Node.js 版本 |
nvm alias default |
将指定的 Node.js 版本设置为默认版本 |
nvm which |
显示指定 Node.js 版本的安装路径 |
nvm root | 显示 NVM 的安装路径 |
nvm version | 显示 NVM 的版本 |
总结
NVM 是一个强大的工具,可以帮助您轻松管理 Node.js 版本。通过使用 NVM,您可以轻松地在不同的项目中使用不同的 Node.js 版本,并轻松地在版本之间切换。这可以提高您的开发效率,并避免因使用不同的 Node.js 版本而导致的问题。