nvm nodejs版本管理工具

编辑于 2024-03-19 17:28:27 阅读 1003

可以很方便地切换 node 版本

安装

macOS or Linux

#在线安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

#下载到本地再安装
cuiwei@weideMacBook-Pro nvm-master % sh install.sh 
=> Downloading nvm from git to '/Users/cuiwei/.nvm'
=> Cloning into '/Users/cuiwei/.nvm'...
remote: Enumerating objects: 345, done.
remote: Counting objects: 100% (345/345), done.
remote: Compressing objects: 100% (292/292), done.
remote: Total 345 (delta 38), reused 170 (delta 28), pack-reused 0
Receiving objects: 100% (345/345), 194.77 KiB | 280.00 KiB/s, done.
Resolving deltas: 100% (38/38), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

导入环境变量

echo 'export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm' >>~/.zshrc

使生效

source ~/.zshrc

Windows

https://github.com/coreybutler/nvm-windows

常用命令

nvm install stable ⬅️安装最新稳定版 node(当前最新稳定版11.6.0)
nvm install <version> ⬅️安装指定版本 (install v10.15.0或install 10.15.0)
nvm uninstall <version> ⬅️卸载指定版本node,(如果删除的为当前使用版本,要解绑,则执行 nvm deactivate)
nvm use <version> ⬅️切换使用指定的版本node
nvm current ⬅️显示当前使用的版本
nvm ls ⬅️列出所有安装的版本
nvm ls available ⬅️列出官网上node的所有版本
nvm alias <name> <version> ⬅️给不同的版本号添加别名
nvm unalias <name> ⬅️删除已定义的别名
nvm alias default <version> ⬅️指定默认版本(设定后需要打开新的终端才生效)
nvm deactivate ⬅️解除当前版本绑定

.nvmrc配置文件

记录当前项目使用的node.js版本

有了.nvmrc文件后,我们在终端没有指定版本时执行 nvm use, nvm install, nvm exec, nvm run, 和 nvm which 命令时会使用 .nvmrc文件指定的版本。

创建 .nvmrc 文件

$ echo "18.12" > .nvmrc
# 设置最新LTS版本
$ echo "lts/*" > .nvmrc 
# 设置最新版本
$ echo "node" > .nvmrc 

卸载

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

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验