在 Pier 中使用 yarn 1.x
Yarn classic——即 yarn 1.22——与 npm 一样从 .npmrc 读取仓库配置。但有一个关键的特殊之处:
always-auth=true是必需的,针对 yarn 1。没有它,yarn 1 会在GET请求中省略 bearer 令牌,导致每次安装都因401 Unauthorized而失败。npm/pnpm/bun 默认会发送 bearer,所以这是 yarn 1 特有的问题。
registry=https://YOUR-PIER-HOST/registry/npm///YOUR-PIER-HOST/registry/npm/:_authToken=pier_npm_…always-auth=trueYarn 1 的某些标志也会从 .yarnrc 读取,但仓库地址和身份验证来自 .npmrc。
yarn installyarn add left-padyarn add @your-org/internal-libyarn publishYarn 1 会以交互方式提示进行版本号提升。在 CI 中可传入 --new-version 1.2.0 以跳过该提示。
Yarn 1 没有自己的登录流程——请对 Pier 使用 npm login,令牌会写入 .npmrc(yarn 1 会读取该文件)。
npm login --auth-type=web --registry=https://YOUR-PIER-HOST/registry/npm/为什么 always-auth=true 很重要
Section titled “为什么 always-auth=true 很重要”Yarn 1 对 GET 请求(包括 packument 获取和 tarball 下载)有一个硬编码的 auth.optional = true 行为。当设置了 always-auth=true 时,yarn 1 会将每个请求(包括 GET)的身份验证提升为必需。
Pier 在每个需要身份验证的路由上(包括读取私有包)都要求进行身份验证。如果没有 always-auth=true,yarn 1 不会发送 bearer → Pier 返回 401 → yarn 失败。
该设置对所有其他客户端都无害,因此始终开启它是安全的。
Node 版本警告
Section titled “Node 版本警告”Yarn 1.22.22 在 Node 20 上存在一个 TLS 握手的特殊问题:在访问 HTTPS 上游时会产生令人困惑的 Request.emit 堆栈跟踪。如果你使用 yarn 1,请使用 Node 22+(理想为 LTS 24)。
nvm install 24nvm use 24yarn --version # → 1.22.22yarn install # 正常工作| yarn 版本 | 状态 | 说明 |
|---|---|---|
| 1.22.x | ✓ | 必须 always-auth=true;使用 Node 22+ |
| 2.x / 3.x / 4.x | ✓ | 配置方式不同——参见 yarn berry 指南 |
Request "https://…/registry/npm/foo" finished with status code 401——缺少always-auth=true。Request.emit (events.js:223:5)堆栈跟踪——yarn 1.22 在 Node 20 上的 TLS bug。请将 Node 升级到 22+ 或 24 LTS。error An unexpected error occurred: "404 Not Found"——上游代理已关闭,且该包未私有发布。请启用代理或私有发布。
- yarn berry (2/3/4) —— 使用不同的配置文件。
- 安装设置 —— 基础配置详解。
- 故障排查 —— 更多 401 场景。