跳到主要内容
ecdsa.com
npm (Node.js)包管理器

错误信息

npm error code ERR_TLS_CERT_ALTNAME_INVALID

也可能输出为

  • npm ERR! code ERR_TLS_CERT_ALTNAME_INVALID
  • npm error request to https://registry.example.com/lodash failed, reason: Hostname/IP does not match certificate's altnames: Host: registry.example.com. is not in the cert's altnames: DNS:www.example.com

npm 10 及更高版本打印“npm error code ERR_TLS_CERT_ALTNAME_INVALID”,npm 9 及更早版本打印“npm ERR! code ERR_TLS_CERT_ALTNAME_INVALID”。原因行中的主机和 altnames 分别来自你的配置和服务器的证书。

npm (Node.js) — registry request over HTTPS

含义

注册表的证书是受信任的,但它不是为 npm 所用注册表 URL 中的主机名签发的。原因行写明了双方:npm 请求的主机,以及证书包含的名称。这种情况常见于注册表迁移到新名称之后,或者注册表 URL 使用了别名、IP 地址或错误的环境。

原因

修复方法

  1. 1.

    将 npm 的注册表设置与证书比对

    打印配置的注册表(以及代理设置)和证书的 SAN 列表。锁文件中也包含 resolved URL,它们必须使用同一个主机。

    bash
    npm config get registry
    npm config list | grep -iE 'registry|proxy'
    echo | openssl s_client -connect registry.example.com:443 -servername registry.example.com 2>/dev/null \
      | openssl x509 -noout -ext subjectAltName
  2. 2.

    让 npm 指向证书上的名称

    更新每一级 .npmrc 中的注册表设置(如果锁文件中包含旧主机,还要重新生成锁文件)。

    bash
    # Use the host name the registry's certificate was issued for
    npm config set registry https://npm.example.com/
  3. 3.

    注册表所有者:覆盖所有正在使用的名称

    重新签发注册表的证书,包含客户端使用的所有主机名,包括为兼容而保留的别名。

    bash
    # Registry owners: include every name clients use
    sudo certbot certonly --nginx -d npm.example.com -d registry.example.com

相关错误

浏览完整的签名错误库