nvm 入门
安装 nvm
# installs NVM (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# 验证是否安装成功
command -v nvm
# 安装系统版本 node
nvm install 20
# verifies the right Node.js version is in the environment
node -v # should print `v20.12.1`
# verifies the right NPM version is in the environment
npm -v # should print `10.5.0`
常用命令
# 远端可用node版本
nvm ls-remote
# 安装并使用最新版node
nvm install node
nvm use node
nvm run node --version
# 使用系统版本
nvm use system
nvm run system --version
# 卸载nvm
nvm_dir="${NVM_DIR:-~/.nvm}"
nvm unload
rm -rf "/Users/sxg/.nvm"
# 清除nvm缓存
nvm cache clear
# 更新nvm
nvm install-latest-npm
# 更新Node.js
nvm install node --reinstall-packages-from=node
# 查看本机安装的 nvm 的安装目录地址
nvm root
node nvm install stable #安装最新稳定版
nvm current #查看当前node版本
nvm alias default v12.13.0 #指定默认的node版本
手动更新
cd "/Users/sxg/.nvm"
git fetch --tags origin
git checkout `git describe --abbrev=0 --tags --match "v0.9.0"
\. "/Users/sxg/.nvm/nvm.sh"
# 查询最新版本
git rev-list --tags --max-count=1
常见 BUG
Failed to connect to raw. githubusercontent. com port 443
错误
If you are behind a proxy, try this:
sudo vim /etc/hosts
add the line below and :wq
199.232.28.133 raw.githubusercontent.com
也可以切换网络后在重试。
nvm ls-remote 结果为 N/A 的解决方案
- 临时请使用导出用于抓取内容的镜像的非 https 版本:
export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist
- 长久解决方案:
若您运行 curl $NVM\_NODEJS\_ORG_MIRROR
出现。
curl: (56) Recv failure: Connection reset by peer
注意❗
可能是vpn导致的
则考虑修改 ~/.nvm/nvm.sh
,在函数 nvm_download()
里修改,加上 curl -k $*
if nvm_has "curl"; then
curl -k $* #新加的
elif nvm_has "wget"; then
# Emulate curl with wget
...
}