错误信息
npm error code DEPTH_ZERO_SELF_SIGNED_CERT
也可能输出为
- npm ERR! code DEPTH_ZERO_SELF_SIGNED_CERT
npm 10 及更高版本打印“npm error code DEPTH_ZERO_SELF_SIGNED_CERT”,npm 9 及更早版本打印“npm ERR! code DEPTH_ZERO_SELF_SIGNED_CERT”。原因行的内容是“self-signed certificate”,在较新的 Node.js 版本上还会附带 --use-system-ca 提示。
npm (Node.js) — registry request over HTTPS
含义
npm 所连接的注册表只出示了一张 Node.js 不信任的自签名证书。这里根本没有 CA 证书链,所以只有两条路:有意地信任这张特定的证书,或者为注册表换上由 CA 签发的证书。这种情况常见于使用自动生成证书启动的本地或自托管注册表。
原因
使用自动生成证书的本地或自托管注册表
常见Verdaccio、Nexus 或 Artifactory 的试用版,或者使用自签名证书启动的注册表容器。
使用出厂证书的专用设备或代理
偶见有些缓存代理和网络设备在安装正式证书之前,会出示自签名证书。
用默认证书响应的 Kubernetes Ingress
偶见如果 Ingress 上没有配置注册表的主机,它就会提供一张自签名的默认证书(ingress-nginx 的是“Kubernetes Ingress Controller Fake Certificate”)。
修复方法
- 1.
只信任这一张证书
获取证书,把它的 SHA-256 指纹与注册表主机上显示的指纹比对,然后通过 NODE_EXTRA_CA_CERTS(或 npm 的 cafile)添加它。
bash # Save the registry's certificate, check its fingerprint, and trust exactly this file openssl s_client -connect registry.example.com:443 -servername registry.example.com </dev/null 2>/dev/null \ | openssl x509 -out registry.pem openssl x509 -in registry.pem -noout -fingerprint -sha256 NODE_EXTRA_CA_CERTS=$PWD/registry.pem npm install - 2.
将私有注册表限定在你的作用域
只把你组织的作用域路由到私有注册表,所有公共包就仍然从 registry.npmjs.org 获取,私有主机上的证书问题也就影响不到它们。
ini ; .npmrc — only @acme packages come from the private registry; the rest from npmjs @acme:registry=https://registry.example.com/ - 3.
为注册表换上由 CA 签发的证书
使用公共 CA(ACME)签发的证书,或者你的机器已经信任的公司 CA 签发的证书,就不再需要为单张证书设置例外。
bash # Give the registry a certificate from a CA your machines trust (public ACME or company CA) sudo certbot certonly --standalone -d registry.example.com