MacOS 安装指定版本的 NodeJS

安装

查找可用的 node 版本

brew searh node

根据需要选择适合的版本,我这里现在 node 12 进行安装

brew install node@12

如果之前安装过 node,可以使用命令 brew uninstall node 卸载掉,再使用 brew unlink node 解绑 node

链接

install 后还不算安装完成,此时在命令行中是无法使用 node 和 npm 的,需要链接安装的node,执行此命令:

brew link node@12

执行成功后会提示:

Linking /usr/local/Cellar/node@12/12.20.2... 3857 symlinks created.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.zshrc

按照提示添加环境变量:

vim ~/.zshrc

# node
export PATH="/usr/local/opt/node@12/bin:$PATH"

完成

在命令中检查版本

node -v

v12.20.2

安装 cnpm

受限于 npm 服务器在国外,下载包时网络速度慢,建议使用淘宝的 npm 镜像,其官方声明:这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。

安装 cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

设置 electron 淘宝镜像

如果开发 electron 的话,建议设置此镜像

npm config set ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/

上述命令修改了用户目录下的文件 .npmrc,追加了:ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/

发表评论