- A+
所属分类:Web前端
环境说明
macOS 13.5
% sw_vers ProductName: macOS ProductVersion: 13.5 BuildVersion: 22G74
Brew
% brew --version Homebrew 4.1.2-33-gc531a35
安装
安装 nvm
使用 brew
安装比较方便,更新一下 Homebrew
并安装
% brew update % brew install nvm ... ... ... You should create NVM's working directory if it doesn't exist: mkdir ~/.nvm Add the following to ~/.zshrc or your desired shell configuration file: export NVM_DIR="$HOME/.nvm" [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion You can set $NVM_DIR to any location, but leaving it unchanged from /usr/local/opt/nvm will destroy any nvm-installed Node installations upon upgrade/reinstall. Type `nvm help` for further information. Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary ? /usr/local/Cellar/nvm/0.35.3: 7 files, 150.0KB, built in 2 seconds
根据提示,创建 .nvm
目录
% mkdir ~/.nvm
编辑 ~/.zshrc
配置文件
% vi ~/.zshrc
在 ~/.zshrc
配置文件后添加如下内容
export NVM_DIR="$HOME/.nvm" [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"
:wq
保存并退出。
使用 :wq
命令使配置生效
% source ~/.zshrc
查看一下配置是否生效
% echo $NVM_DIR /Users/your-username/.nvm
查看帮助
% nvm --help ... ... Note: to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
帮助的最后一句话,可以看到删除 nvm 很简单,只要删除 $NVM_DIR
文件夹既可以
安装 node.js
查看 node.js
版本可以使用如下命令
% nvm ls-remote
可以看到很多版本,接下来安装 LTS 版本
% nvm install 16 Downloading and installing node v16.20.1... ... Computing checksum with shasum -a 256 Checksums matched!
安装完之后,可以查看一下
% nvm ls -> v16.20.1 default -> 16 (-> v16.20.1) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v18.17.0) (default) stable -> 18.17 (-> v18.17.0) (default) lts/* -> lts/hydrogen (-> v18.17.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.1
可以看到,安装的是 v16 的稳定版。因为只安装了一个版本,所以使用的版本是 v16
使用其他版本(建议安装稳定版本), 本例安装 v18 的稳定版为例
% nvm install 18 Downloading and installing node v18.17.0... ... Computing checksum with shasum -a 256 Checksums matched!
可以看到当前 node 版本改为 v18
% nvm ls v16.20.1 -> v18.17.0 default -> 16 (-> v16.20.1) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v18.17.0) (default) stable -> 18.17 (-> v18.17.0) (default) lts/* -> lts/hydrogen (-> v18.17.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.1 lts/hydrogen -> v18.17.0
切换默认的版本
% nvm use 18 Now using node v18.17.0 (npm v9.6.7)
查看一下版本
% node -v v18.17.0