📒
notebook
  • Notebook
  • DevOps
    • Git
      • 常见 Git 操作
      • 使用 SSH 连接 Git 远程仓库
      • 使用 GPG Keys 签名 Git 提交
      • Git on macOS
    • Docker
      • macOS 安装配置 Docker
      • CentOS7 安装配置 Docker CE
  • Linux
    • CentOS
      • CentOS7 安装配置 vsftpd
      • CentOS7 设置虚拟内存
      • CentOS7 使用 acme.sh 自动申请免费 SSL 证书
      • CentOS7 修改 SSH 端口号
      • CentOS7 主机初始设置
      • 阿里云 CentOS 主机常见设置
      • CentOS7 安装配置 SS
    • nginx
      • nginx 配置 301 永久重定向
      • nginx 使用 SSL证书配置 HTTPS
      • CentOS7 安装配置 nginx
      • nginx 配置 gzip 压缩
      • nginx 代理静态网页
  • Server
    • Ghost
      • macOS 安装配置 Ghost
      • CentOS7 安装配置 Ghost
    • npm & Yarn
      • Yarn 常用命令
      • CentOS7 安装卸载升级 Yarn
      • npm & Yarn 常见错误处理
      • macOS 安装卸载升级 Yarn
    • Node.js
      • Awesome Node.js
      • CentOS7 安装卸载升级 Node.js
      • macOS 安装卸载升级 Node.js
  • Web
    • Ionic
      • 创建 Ionic & Angular 项目
      • 使用 Ionic & Cordova 构建 Android 应用
      • macOS 搭建 Ionic & Cordova 开发环境
    • CSS
      • CSS 排版技巧
      • Awesome CSS
      • CSS 三栏自适应布局
    • Angular
      • Awesome Angular
      • 创建 Angular 项目
    • HTML
      • HTML head 常用标签
      • HTML 常用 DTD 声明
      • 常用网页语义结构
    • Web 技术标准
    • JavaScript
      • Awesome JavaScript
      • JavaScript 的 eval() 函数详解
  • Mobile
    • H5
      • iOS Safari Web App 配置
  • Development Environment
    • Development Utilities
      • Awesome Windows
      • macOS 安装配置 Homebrew
      • Awesome macOS
      • macOS 安装配置 iTerm2
    • FEED
      • 常用 Gulp 插件
  • Network
    • 常见公共 DNS
  • Technology Stacks for Web Front-End Development
Powered by GitBook
On this page
  • 安装 Yarn
  • 切换 Yarn 源
  • 手动切换 Yarn 源
  • 使用 yrm 切换 Yarn 源
  • 卸载 Yarn
  • 升级 Yarn
  • 常见问题
  • 安装失败
  • 找不到命令
  • 参考文献

Was this helpful?

  1. Server
  2. npm & Yarn

macOS 安装卸载升级 Yarn

安装 Yarn

使用 Homebrew 安装 Yarn:

brew install yarn --ignore-dependencies

查看安装的 Yarn 版本:

yarn -v

1.17.3

查看 Yarn 全局 bin 路径:

yarn global bin

/usr/local/bin

查看当前用户的环境变量中的 $PATH :

echo $PATH

/usr/local/opt/node@10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

可见 Yarn 全局 bin 路径在其中,不需要设置。

如果不在其中,则需要手动添加路径至 Zsh 配置文件 ~/.zshrc 中:

echo 'export PATH="$PATH:`yarn global bin`"' >> ~/.zshrc
source ~/.zshrc

Tips: 如果使用的是 macOS 默认的 bash,则配置文件为 ~/.bash_profile 。

切换 Yarn 源

手动切换 Yarn 源

常见 Node.js 软件源:

# npm(推荐海外使用)
https://registry.npmjs.org/

# taobao(推荐国内使用)
https://registry.npm.taobao.org/

手动切换 Yarn 源,如切换至 taobao 镜像源 :

yarn config set registry https://registry.npm.taobao.org/

查看设置之后的 Yarn 源配置:

yarn config get registry

使用 yrm 切换 Yarn 源

全局安装 yrm :

sudo yarn global add yrm

列出 yrm 支持的源:

yrm ls

测试各个源的连接速度:

yrm test
  • npm ---- 494ms

    cnpm --- 311ms

    taobao - 110ms

    nj ----- Fetch Error

    rednpm - Fetch Error

    npmMirror 1583ms

    edunpm - Fetch Error

    yarn --- Fetch Error

切换至速度最快的的源,如 taobao:

yrm use taobao

卸载 Yarn

查看当前所有已安装软件包依赖树:

brew deps --installed --tree

icu4c

node@10 └── icu4c

yarn └── node └── icu4c

可见有共享依赖 icu4c ,只卸载 Yarn 软件包:

brew uninstall yarn

升级 Yarn

更新 Homebrew:

brew update

卸载并重装 Yarn 完成升级:

brew uninstall yarn && brew install yarn --ignore-dependencies

Tips: 使用brew upgrade yarn 会强制安装最新版 node,且该命令不支持 --ignore-dependencies 参数,只能卸载了重装。

常见问题

安装失败

错误信息

如果 Node.js 是使用 Homebrew 安装的非最新版本,比如 node@12 ,后续使用 Homebrew 安装 Yarn 即使使用了 --ignore-dependencies 参数依然可能会安装失败:

...... Error: An exception occurred within a child process: RuntimeError: /usr/local/opt/node not present or broken Please reinstall node. Sorry :(

解决办法

手动建立软链:

ln -s /usr/local/opt/node@12 /usr/local/opt/node

再重新安装:

brew install yarn --ignore-dependencies

找不到命令

错误信息

macOS 上使用 Homebrew 安装的 Yarn,可能会偶然出现找不到命令的情况(之前正常):

zsh: command not found: yarn

使用 Homebrew 查看安装的软件列表,发现安装的有 Yarn:

brew list

gettext git gradle icu4c node@12 pcre2 yarn

解决办法

重新建立一次 Yarn 命令的软链即可:

brew unlink yarn && brew link yarn

参考文献

Previousnpm & Yarn 常见错误处理NextNode.js

Last updated 4 years ago

Was this helpful?

npm ----

cnpm ---

taobao -

nj -----

rednpm -

npmMirror

edunpm -

yarn ---

https://registry.npm.taobao.org/
https://registry.npmjs.org/
http://r.cnpmjs.org/
https://registry.npm.taobao.org/
https://registry.nodejitsu.com/
http://registry.mirror.cqupt.edu.cn/
https://skimdb.npmjs.com/registry/
http://registry.enpmjs.org/
https://registry.yarnpkg.com
安装 Yarn