错误信息
error Error: unable to get local issuer certificate
也可能输出为
- YN0001: RequestError: unable to get local issuer certificate
Yarn 1.x 会打印“error Error: unable to get local issuer certificate”,后面跟着 Node.js 堆栈跟踪。如果注册表只发送叶子证书,文本则会变成“unable to verify the first certificate”。
含义
Yarn Classic 从注册表收到了证书链(叶子证书和中间证书),但 Node.js 不信任其上方的根证书。Yarn 会原样显示 Node.js 的错误。注册表通常没有问题;是这台机器缺少证书链所通向的公司根证书或代理根证书。
原因
私有注册表的证书由公司 CA 签发
常见Artifactory、Nexus 或 Verdaccio 使用内部 CA 签发的证书,而 Node.js 不认识该 CA 的根证书。
执行 TLS 检查的代理发送了它用来签发证书的中间证书
常见如果代理发送了叶子证书和中间证书,却没有发送自己的根证书,报出的就是这个错误码,而不是“self-signed certificate in certificate chain”。
锁文件指向另一个注册表主机
偶见yarn.lock 保存的是完整的 tarball URL。即使注册表设置已经更改,针对私有镜像生成的锁文件仍会继续从该主机下载。
修复方法
- 1.
设置 Yarn 1 的 cafile
让 Yarn 指向公司根证书(或者指向同时包含公共根证书的证书包,因为对注册表请求而言,cafile 会取代默认的根证书)。
bash yarn config set cafile /etc/ssl/private-ca/corp-root-ca.pem - 2.
或者使用 NODE_EXTRA_CA_CERTS
把根证书添加到 Node.js 的内置列表中,对 Yarn 以及其他任何设置了该变量的 Node.js 进程都生效。
bash export NODE_EXTRA_CA_CERTS=/etc/ssl/private-ca/corp-root-ca.pem yarn install --frozen-lockfile - 3.
Yarn 4:httpsCaFilePath
Yarn 4 报出的错误是“YN0001: RequestError: unable to get local issuer certificate”,CA 文件则从 .yarnrc.yml 中的 httpsCaFilePath 读取。
yaml # Yarn 4: .yarnrc.yml httpsCaFilePath: /etc/ssl/private-ca/corp-root-ca.pem - 4.
检查锁文件使用了哪些主机
如果 resolved 字段中的 URL 指向内部镜像,需要修复的是该镜像的证书或你的信任库,而不是注册表设置。
bash # Which hosts does the lockfile download from? grep -oE 'resolved "https://[^/]+' yarn.lock | sort | uniq -c