MacOS 一步完成 node、nvm、npm、yarn、pnpm 安装
以
v20.18.1
为例
1. 安装流程
- 安装nvm(Node 版本管理器)
# 安装 nvm (Node 版本管理器)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# 验证环境中是否存在正确的 nvm 版本
nvm -v # 应该打印 `0.40.1`
- 安装node和npm
# 下载并安装 Node.js(可能需要重启终端)
nvm install 20
# 验证环境中是否存在正确的 Node.js 版本
node -v # 应该打印 `v20.18.1`
# 验证环境中是否存在正确的 npm 版本
npm -v # 应该打印 `10.8.2`
- 安装yarn
# 下载并安装 yarn(可能需要重启终端)
npm install --global yarn
# 查看版本号
yarn -v
- 安装pnpm
# 下载并安装 pnpm(可能需要重启终端)
npm install --global pnpm
# 查看版本号
pnpm -v
2. 安装异常或使用问题问题
- nvm安装问题
安装时出现:
Failed to connect to raw.githubusercontent.com port 443: Connection refused
查看解决办法node管理工具nvm在mac上的安装以及失败问题解决
- nvm切换node版本问题
# 仅对当前终端窗口有效
nvm use [version]
# 当前窗口或新窗口都有效
nvm alias default [version]
- 配置默认镜像源
# 查询当前npm镜像源
npm get registry
# 设置为淘宝镜像源
npm config set registry https://registry.npmmirror.com
# 还原为官方镜像源
npm config set registry https://registry.npmjs.org
# 查询当前yarn镜像源
yarn config get registry
# 设置为淘宝镜像源
yarn config set registry https://registry.npmmirror.com
# 还原为官方镜像源
yarn config set registry https://registry.yarnpkg.com
# 查询当前pnpm镜像源
pnpm get registry
# 设置为淘宝镜像源
pnpm config set registry https://registry.npmmirror.com
# 还原为官方镜像源
pnpm config set registry https://registry.npmjs.org