<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>npm证书过期 &#8211; SSL证书知识</title>
	<atom:link href="https://www.sslgood.com/tags/npm%e8%af%81%e4%b9%a6%e8%bf%87%e6%9c%9f/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sslgood.com</link>
	<description>SSL证书知识</description>
	<lastBuildDate>Thu, 03 Dec 2026 18:19:05 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>npm 证书过期报错 CERT_HAS_EXPIRED 怎么解决？根因、配置与排查全流程</title>
		<link>https://www.sslgood.com/npm-%e8%af%81%e4%b9%a6%e8%bf%87%e6%9c%9f%e6%8a%a5%e9%94%99-cert_has_expired-%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3%ef%bc%9f%e6%a0%b9%e5%9b%a0%e3%80%81%e9%85%8d%e7%bd%ae%e4%b8%8e%e6%8e%92%e6%9f%a5/</link>
		
		<dc:creator><![CDATA[sslgood]]></dc:creator>
		<pubDate>Thu, 03 Dec 2026 18:19:05 +0000</pubDate>
				<category><![CDATA[错误排查]]></category>
		<category><![CDATA[CERT_HAS_EXPIRED]]></category>
		<category><![CDATA[Node.js CA]]></category>
		<category><![CDATA[npm cafile]]></category>
		<category><![CDATA[npm证书过期]]></category>
		<category><![CDATA[SSL证书排查]]></category>
		<category><![CDATA[企业代理TLS]]></category>
		<guid isPermaLink="false">https://www.sslgood.com/npm-%e8%af%81%e4%b9%a6%e8%bf%87%e6%9c%9f%e6%8a%a5%e9%94%99-cert_has_expired-%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3%ef%bc%9f%e6%a0%b9%e5%9b%a0%e3%80%81%e9%85%8d%e7%bd%ae%e4%b8%8e%e6%8e%92%e6%9f%a5/</guid>

					<description><![CDATA[npm 安装依赖时报 CERT_HAS_EXPIRED？本文从 Node 内置 CA、企业代理 TLS 拦截、系统时间与 npm cafile 配置四个根因讲起，给出 Node 18/20/22 与 npm 10 的实操命令、参数对比表、真实报错排查和验证方法，帮助运维与站长恢复 npm install。]]></description>
										<content:encoded><![CDATA[<h2>一、问题背景与根本原因：npm 证书过期为什么频繁出现？</h2>
<p><strong>先看清报错长什么样，再动手。</strong>在 npm 10.8.2 + Node 18.20.4 的终端里，访问公共仓库时最常见的一组输出是：</p>
<pre><code>npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npmjs.org/npm failed, reason: certificate has expired
</code></pre>
<p>注意最后一行：它由 Node 的 TLS 层抛出，而不是 npm 自己判定的。可用一条命令稳定复现：<code>npm view npm version --registry=https://registry.npmjs.org</code>。此时<strong>不要急着重装 Node</strong>——重装经常换回同一个旧镜像，问题原样复现。</p>
<p><strong>为什么 npm 自己不修证书？</strong>因为 npm 不维护独立 CA 库，它复用 Node.js 的 TLS 栈：默认信任列表来自编译进二进制的 Mozilla CA bundle（可通过 <code>require('tls').rootCertificates</code> 读取），外加 <code>NODE_EXTRA_CA_CERTS</code> 指定的额外根证书。Node 18.20.4、20.15.1、22.4.1 是不同时间构建的发行包，内置 CA 快照并不同步；基于旧基础镜像（CA 包停留在 2021 年）的容器，最容易把一张仍在有效期内的证书误判为过期——这正是 npm 证书过期在 CI 里反复出现的第一类原因。</p>
<p><strong>第二类是企业代理的 TLS 拦截。</strong>代理用企业根证书动态为 registry.npmjs.org 重签证书，Node 未信任该根证书时，报错会在 <code>CERT_HAS_EXPIRED</code> 与 <code>SELF_SIGNED_CERT_IN_CHAIN</code> 之间摇摆。据 Mozilla CA 证书计划，根证书必须被显式导入信任库才生效，操作系统信任 ≠ Node 信任。所以第一步永远是看签发者：</p>
<pre><code># 查看 registry.npmjs.org 实际返回的证书链签发者与有效期
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org -showcerts /dev/null 
  | openssl x509 -noout -issuer -subject -dates
# issuer 是公共 CA（如 DigiCert/Let's Encrypt）→ 查本机 CA 与时间
# issuer 是公司名称 → 代理拦截，需导入企业根证书，而非关闭校验
</code></pre>
<p><strong>第三类是系统时间漂移。</strong>有效期校验是双向的，偏差超过 5 分钟即失败：</p>
<pre><code>date -u                                              # 容器内 UTC 时间
curl -sI https://registry.npmjs.org | grep -i '^date:'   # 服务端时间基准
# 两者相差 &gt;300 秒，先修 NTP/chrony（容器需 --cap-add=SYS_TIME 或同步宿主时钟）
</code></pre>
<p><strong>第四类是历史遗留的根证书问题。</strong>据 Let&#8217;s Encrypt 文档，ISRG Root X1 的交叉签名于 2021-09-30 到期，仍持有旧根证书的客户端访问相关站点会直接报过期。公共 npm 仓库由公共 CA 签发，一旦 issuer 显示公共 CA 却依然报错，问题几乎必然落在本机信任库或时间上。</p>
<p><strong>第五类是配置残留制造的假象。</strong>下面四项默认值与现状必须逐条确认：</p>
<pre><code>npm config get strict-ssl   # 默认 true；为 false 说明有人关过校验
npm config get cafile       # 默认 null；指向已过期 PEM 会污染整条信任链
npm config get proxy        # 默认 null
npm config get https-proxy  # 默认 null；指向失效代理会在握手阶段直接失败
</code></pre>
<table>
<thead>
<tr>
<th>报错码</th>
<th>最可能根因</th>
<th>首个验证命令</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>CERT_HAS_EXPIRED</code></td>
<td>系统时间偏差 / 内置 CA 快照过旧 / 代理重签证书已过期</td>
<td><code>date -u</code> 与 <code>curl -sI</code> 的 Date 头对比</td>
</tr>
<tr>
<td><code>SELF_SIGNED_CERT_IN_CHAIN</code></td>
<td>企业代理根证书未被 Node 信任</td>
<td><code>openssl s_client ... -showcerts</code> 看 issuer</td>
</tr>
<tr>
<td><code>UNABLE_TO_VERIFY_LEAF_SIGNATURE</code></td>
<td>中间证书缺失或 cafile 配置错误</td>
<td><code>npm config get cafile</code></td>
</tr>
<tr>
<td><code>ETIMEDOUT</code> / <code>ECONNREFUSED</code></td>
<td>proxy/https-proxy 指向失效代理</td>
<td><code>npm config get https-proxy</code></td>
</tr>
</tbody>
</table>
<p>把这张表当成入口：先用一条 <code>openssl s_client</code> 区分“公共 CA”与“公司代理 CA”，再决定是补 CA、校时，还是改代理配置——顺序反了，就会在重装 Node 上白耗时间。</p>
<h2>二、完整实操步骤：从定位到修复（分步骤编号）</h2>
<p>遇到 <code>npm ERR! code CERT_HAS_EXPIRED</code> 时，先别急着关掉校验——这个错误码来自 Node 底层 OpenSSL 的证书链验证，含义是&#8221;链上某张证书的 notAfter 已过&#8221;。要定位它到底是公网证书真的过期、还是企业代理替换了证书，按下面六步走，通常 15 分钟内能收敛。据 OpenSSL 文档，<code>X509_V_ERR_CERT_HAS_EXPIRED</code> 只在验证链中任一级证书超期时触发，因此逐级抓链比猜测有效。</p>
<p><strong>第 1 步：固定环境信息。</strong>先记录版本，否则后面所有结论都无法复现。npm 的 <code>strict-ssl</code> 默认为 <code>true</code>，<code>cafile</code> 默认为 <code>null</code>（即使用 Node 内置 CA 包），<code>proxy</code> 与 <code>https-proxy</code> 默认均为 <code>null</code>，<code>registry</code> 默认 <code>https://registry.npmjs.org/</code>。</p>
<pre><code># 记录版本与关键配置，输出贴进工单便于复现
node -v
npm -v
npm config list -l | grep -E 'strict-ssl|cafile|proxy|registry'
# 关注 strict-ssl=true、cafile=null 是否为预期值</code></pre>
<table>
<thead>
<tr>
<th>Node 版本</th>
<th>配套 npm</th>
<th>建议</th>
</tr>
</thead>
<tbody>
<tr>
<td>16.20.2</td>
<td>8.19.4</td>
<td>最低可接受，优先升级</td>
</tr>
<tr>
<td>18.20.4 LTS</td>
<td>10.8.2</td>
<td>推荐基线</td>
</tr>
<tr>
<td>20.15.1 LTS</td>
<td>10.8.2</td>
<td>推荐基线</td>
</tr>
<tr>
<td>22.4.1</td>
<td>10.8.2</td>
<td>推荐基线</td>
</tr>
</tbody>
</table>
<p><strong>第 2 步：校时。</strong>时钟偏移超过证书有效期窗口时会先报 <code>CERT_NOT_YET_VALID</code>，但若代理根证书本身临近到期，偏移几百秒同样会把&#8221;还没过期&#8221;变成&#8221;已过期&#8221;。Linux 主机用 <code>timedatectl status</code> 看 <code>System clock synchronized: yes</code>，用 <code>chronyc tracking</code> 看 <code>System time</code> 偏差；容器内直接 <code>date -u</code>，再与 registry 响应头对比：</p>
<pre><code># 取 registry 的 Date 响应头，与本地 UTC 时间比较
curl -sI https://registry.npmjs.org | grep -i '^date:'
date -u</code></pre>
<p>偏差大于 300 秒就先修时间（<code>timedatectl set-ntp true</code> 或容器挂载宿主 <code>/etc/localtime</code>），否则后面导入根证书也会失败。</p>
<p><strong>第 3 步：抓证书链，看 issuer。</strong>这一步是判断&#8221;真过期&#8221;还是&#8221;被拦截&#8221;的分水岭：</p>
<pre><code># 抓取服务端返回的第一张证书，打印有效期与签发者
openssl s_client -connect registry.npmjs.org:443 
  -servername registry.npmjs.org -showcerts /dev/null 
  | openssl x509 -noout -dates -issuer -subject</code></pre>
<p>正常输出中 <code>issuer</code> 应为公开 CA（如 <code>C=US, O=DigiCert Inc</code> 或 Let&#8217;s Encrypt 的 <code>ISRG Root X1</code>）。如果 issuer 是公司名称，基本可确定是企业代理做了 TLS 拦截——据 Mozilla 的 CA 证书存储政策，这类自签根证书永远不在 Node 内置信任列表里。</p>
<p><strong>第 4 步：确认代理链路。</strong></p>
<pre><code># 三处都可能设代理，逐一确认
env | grep -i proxy
npm config get proxy
npm config get https-proxy
# 观察 curl 看到的证书 subject 是否已被替换
curl -vI https://registry.npmjs.org 2&gt;&amp;1 | grep -i 'subject|issuer'</code></pre>
<p><strong>第 5 步：只导入根证书。</strong>把企业根证书存为 PEM（如 <code>/etc/ssl/certs/corp-root-ca.pem</code>），两种方式语义不同，务必分清：</p>
<pre><code># 方式 A（推荐）：追加到内置 CA 列表，Node 7.3.0+ 支持，进程级生效
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem

# 方式 B：仅对 npm 生效，注意 cafile 是"替换"而非"追加"
npm config set cafile /etc/ssl/certs/corp-root-ca.pem</code></pre>
<table>
<thead>
<tr>
<th>方式</th>
<th>作用范围</th>
<th>是否替换内置 CA</th>
<th>适用场景</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>NODE_EXTRA_CA_CERTS</code></td>
<td>该 Node 进程及其子进程</td>
<td>否，追加</td>
<td>推荐，CI 与本地通用</td>
</tr>
<tr>
<td><code>npm config set cafile</code></td>
<td>仅 npm</td>
<td>是，需提供完整 CA 包</td>
<td>只有 npm 走代理时</td>
</tr>
<tr>
<td><code>strict-ssl=false</code></td>
<td>仅 npm</td>
<td>跳过校验</td>
<td>仅限 10 分钟隔离验证</td>
</tr>
</tbody>
</table>
<p>注意 Node 默认使用编译进二进制的 Mozilla CA 包，<strong>不读</strong>系统 <code>/etc/ssl/certs</code>，除非以 <code>--use-openssl-ca</code> 启动；因此 <code>update-ca-certificates</code> 往往对 npm 无效，这正是很多人&#8221;系统已信任但 npm 仍报 npm 证书过期&#8221;的原因。切勿把叶子证书或中间证书单独塞进去。</p>
<p><strong>第 6 步：清理缓存并复测。</strong></p>
<pre><code># 清掉可能缓存了旧证书校验结果的元数据
npm cache clean --force
npm install --registry=https://registry.npmjs.org --loglevel=verbose

# 若仍失败：临时关闭校验做隔离验证，10 分钟内必须回滚
npm config set strict-ssl false
npm config delete strict-ssl
npm config get strict-ssl   # 确认已回到 true</code></pre>
<p>如果 <code>--loglevel=verbose</code> 输出的 <code>npm http fetch GET</code> 行中仍出现 <code>CERT_HAS_EXPIRED</code>，说明根证书没生效（检查 <code>NODE_EXTRA_CA_CERTS</code> 是否在当前 shell 导出、文件是否为 PEM 且含 <code>BEGIN CERTIFICATE</code>）；若变成 <code>SELF_SIGNED_CERT_IN_CHAIN</code>，则说明链条中间证书缺失，需要向网络团队索取完整的根+中间证书。</p>
<h2>三、配置与命令示例：Node 18/20/22 与 npm 10 可复制片段</h2>
<p>先把默认值摸清楚，再动手改——很多&#8221;npm证书过期&#8221;排查最后发现是把 <code>cafile</code> 设成了只含企业根证书的单个文件，结果公网 registry 反而校验失败。npm 10（随 Node 18/20/22 发布）的四个关键默认值可以直接查：</p>
<pre><code># npm 10.x / Node 18+ 均可直接执行；未设置时返回 undefined（npm 8 及更早返回 null）
npm config get strict-ssl      # true
npm config get cafile          # undefined —— 即空，表示沿用 Node 内置 CA
npm config get registry        # https://registry.npmjs.org/
npm config get fetch-timeout   # 300000（毫秒）
# 弱网或代理链路慢时可下调：npm config set fetch-timeout 60000
</code></pre>
<p>四个方案的作用域差别很大，选错就会互相覆盖，据 npm 官方文档与 Node.js 官方文档，它们的行为对比如下：</p>
<table>
<thead>
<tr>
<th>方案</th>
<th>生效范围</th>
<th>对内置 CA 的影响</th>
<th>回滚命令</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>npm config set cafile</code></td>
<td>仅 npm</td>
<td><strong>替换</strong>默认信任链，文件必须包含完整 bundle</td>
<td><code>npm config delete cafile</code></td>
</tr>
<tr>
<td><code>NODE_EXTRA_CA_CERTS</code></td>
<td>整个 Node 进程（npm、pnpm、Vite 等）</td>
<td><strong>只追加</strong>，不替换内置 CA</td>
<td><code>unset NODE_EXTRA_CA_CERTS</code></td>
</tr>
<tr>
<td><code>strict-ssl false</code></td>
<td>仅 npm</td>
<td>完全关闭校验</td>
<td><code>npm config delete strict-ssl</code></td>
</tr>
<tr>
<td>系统 CA store</td>
<td>全系统（含 curl、openssl）</td>
<td>追加</td>
<td>删除证书后重跑 <code>update-ca-certificates</code></td>
</tr>
</tbody>
</table>
<p><strong>npm 级 cafile。</strong>适合&#8221;只有 npm 需要私有 CA&#8221;的场景，设置后务必回读确认写入的是同一路径：</p>
<pre><code># 把企业根证书写入 npm 配置（会写进 ~/.npmrc）
npm config set cafile /etc/ssl/certs/corp-root-ca.pem
npm config get cafile    # 应原样返回 /etc/ssl/certs/corp-root-ca.pem
# 回滚
npm config delete cafile
</code></pre>
<p>注意：<code>cafile</code> 是替换而非追加，若企业同时需要访问公网 registry，应指向合并了系统 bundle 与企业根证书的文件（如 <code>cat /etc/ssl/certs/ca-certificates.crt corp-root-ca.pem &gt; bundle.pem</code>），漏掉公共根证书就会继续报 <code>CERT_HAS_EXPIRED</code> 或 <code>UNABLE_TO_VERIFY_LEAF_SIGNATURE</code>。</p>
<p><strong>Node 级追加信任。</strong>据 Node.js 官方文档，<code>NODE_EXTRA_CA_CERTS</code> 只追加、不替换内置 CA，是兼容性最好的做法：</p>
<pre><code># Linux / macOS：仅当前 shell 生效
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem
# Windows：写入用户环境变量，需重开终端才生效
setx NODE_EXTRA_CA_CERTS "C:/certs/corp-root-ca.pem"
# 验证根证书确实被加载（OpenSSL 文档中的 -CAfile 参数）
openssl s_client -connect registry.npmjs.org:443 -CAfile /etc/ssl/certs/corp-root-ca.pem &lt;/dev/null
</code></pre>
<p><strong>代理配置。</strong>证书过期报错常被误判，实际是代理做了 TLS 中间人：</p>
<pre><code># 写入 ~/.npmrc；带认证时可用 http://user:pass@proxy.corp.example:8080
npm config set proxy http://proxy.corp.example:8080
npm config set https-proxy http://proxy.corp.example:8080
# 注意：明文密码会落在 ~/.npmrc，CI 中改用环境变量更安全
export HTTP_PROXY=http://proxy.corp.example:8080
export HTTPS_PROXY=http://proxy.corp.example:8080
</code></pre>
<p><strong>临时关闭校验。</strong>只建议在隔离环境短时使用，用完立刻恢复，否则这条 npm 证书过期&#8221;修复&#8221;会变成长期安全债：</p>
<pre><code>npm config set strict-ssl false
# 排查完成后务必恢复
npm config set strict-ssl true   # 或 npm config delete strict-ssl
</code></pre>
<p><strong>容器 / CI 片段。</strong>镜像里没有企业根证书是最常见的根因：</p>
<pre><code># Debian 系基础镜像（node:20-bookworm-slim）
COPY corp-root-ca.pem /usr/local/share/ca-certificates/corp-root-ca.crt
RUN update-ca-certificates
ENV NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem

# Alpine（node:20-alpine）需先装 ca-certificates 包
RUN apk add --no-cache ca-certificates
COPY corp-root-ca.pem /usr/local/share/ca-certificates/corp-root-ca.crt
RUN update-ca-certificates
</code></pre>
<p>容器内可用 <code>npm config get cafile</code> 与 <code>node -e "console.log(process.env.NODE_EXTRA_CA_CERTS)"</code> 双重确认配置是否真正进入运行时，避免构建缓存导致改动未生效。</p>
<h2>四、参数对比与方案选型表格：先修信任链，再谈绕过</h2>
<p><strong>问：六种办法摆在面前，到底该选哪一个？</strong></p>
<p>先说结论：<strong>先修信任链，再谈绕过</strong>。出现 <code>npm证书过期</code> 报错时，绝大多数情况是本地信任链里缺了或旧了某张根证书/中间证书，而不是 npm 本身有 bug；据 OpenSSL 官方文档，<code>verify error:num=10:certificate has expired</code> 描述的就是链上某张证书超出 notAfter，只有把正确的根证书喂给 Node，才是真正修复。</p>
<table>
<thead>
<tr>
<th>方案</th>
<th>适用场景</th>
<th>关键命令/参数</th>
<th>默认值/版本</th>
<th>风险与回滚</th>
</tr>
</thead>
<tbody>
<tr>
<td>升级 Node/npm</td>
<td>本地内置 CA 包过旧</td>
<td><code>nvm install 20.15.1</code>；<code>npm i -g npm@10.8.2</code></td>
<td>基线 Node 18.20.4+/20.15.1+</td>
<td>低；<code>nvm use</code> 可回退</td>
</tr>
<tr>
<td>npm cafile</td>
<td>企业代理根证书</td>
<td><code>npm config set cafile /etc/ssl/certs/corp-root-ca.pem</code></td>
<td>默认空</td>
<td>中；<code>npm config delete cafile</code></td>
</tr>
<tr>
<td>NODE_EXTRA_CA_CERTS</td>
<td>单个 Node 进程 / CI</td>
<td><code>export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem</code></td>
<td>Node 18.20.4+ 支持</td>
<td>低；<code>unset</code> 即回滚</td>
</tr>
<tr>
<td>代理环境变量</td>
<td>代理需认证</td>
<td><code>export HTTPS_PROXY=http://user:pass@proxy:8080</code></td>
<td>无默认值</td>
<td>中；切勿写入仓库</td>
</tr>
<tr>
<td>strict-ssl=false</td>
<td>10 分钟隔离验证</td>
<td><code>npm config set strict-ssl false</code></td>
<td>默认 true</td>
<td>高；验证后立即恢复</td>
</tr>
<tr>
<td>换 registry 镜像</td>
<td>跨境网络慢</td>
<td><code>npm config set registry https://registry.npmmirror.com/</code></td>
<td>默认 registry.npmjs.org</td>
<td>中；注意镜像同步延迟</td>
</tr>
</tbody>
</table>
<p><strong>问：<code>cafile</code> 和 <code>NODE_EXTRA_CA_CERTS</code> 能随便互换吗？</strong></p>
<p>不能，这是最容易踩的坑。npm 的 <code>cafile</code>（底层是 <code>ca</code> 选项）会<strong>替换</strong>默认 CA 列表，只放企业根证书会导致访问公共仓库时反过来报 <code>SELF_SIGNED_CERT_IN_CHAIN</code>；而 Node 的 <code>NODE_EXTRA_CA_CERTS</code> 是<strong>追加</strong>到默认信任库，更安全。验证两者是否生效：</p>
<pre><code># 查看当前 Node 版本与内置 OpenSSL 版本（低于 16.20.2 请先升级）
node -e "console.log(process.versions.node, process.versions.openssl)"

# 打印服务端证书链，确认到底哪一张过期
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org -showcerts &lt;/dev/null 2&gt;/dev/null | openssl x509 -noout -dates -subject

# 追加企业根证书（仅当前 shell 生效，CI 里最推荐）
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem
npm ping   # 返回 PONG 即信任链已通
</code></pre>
<p><strong>选型顺序</strong>：先校时（<code>date -u</code>，系统时间偏差超过几分钟同样触发过期判定）与升级 Node/npm，这一步 0 成本；再配 <code>cafile</code> 或 <code>NODE_EXTRA_CA_CERTS</code>，也是 0 成本；最后才考虑 <code>strict-ssl=false</code>。据 Mozilla CA 证书计划说明，关闭校验后浏览器与 Node 都会静默接受任何自签名证书，中间人攻击完全不可见，因此不建议开启超过 10 分钟。</p>
<p><strong>版本基线与成本</strong>：Node 18.20.4 LTS、20.15.1 LTS、22.4.1，npm 10.8.2，OpenSSL 3.0.13（具体以 Node 官方 release notes 为准）。企业根证书分发 0 元；公共 CA 商业证书约 1000–5000 元/年，但 npm 公共仓库用的是公共信任链，别被“买张证书修 npm”带偏。</p>
<p><strong>回滚策略</strong>：改配置前先备份，回滚用 delete 而不是手改文件。</p>
<pre><code># 改之前留一份带日期的备份
cp ~/.npmrc ~/.npmrc.bak.$(date +%F)

# 回滚三连（按需执行）
npm config delete cafile
npm config delete proxy
npm config set strict-ssl true

# 确认最终生效值，改完务必重开终端
npm config get strict-ssl cafile registry
</code></pre>
<h2>五、常见错误与排查：真实报错怎么读</h2>
<p>报错文本本身不会骗人，但同一段 <code>certificate has expired</code> 可能来自三个完全不同的原因：服务端证书真的过期、本机信任链里塞了一张过期的中间证书、或者系统时钟跑到了证书生效之前。下面的排查顺序是按&#8221;从报错到根因的平均耗时&#8221;排的，先跑第一条命令再决定往下走。</p>
<p><strong>问：只看到 <code>npm ERR! code CERT_HAS_EXPIRED</code> 和 <code>certificate has expired</code>，怎么在 30 秒内分清是证书过期还是时钟错误？</strong></p>
<p>答：直接读服务端返回的 <code>notAfter</code> 与 <code>issuer</code>。RFC 5280 规定证书有效期由 notBefore/notAfter 两个字段界定，客户端校时错误会表现为&#8221;未来证书&#8221;，而不是过期。命令必须带 <code>-servername</code>，否则在共享 IP 的 CDN 上会拿到默认站点证书，白排查一场。</p>
<pre><code># 打印 registry.npmjs.org 实际返回的证书主体、签发者与有效期
openssl s_client -connect registry.npmjs.org:443 
  -servername registry.npmjs.org -showcerts &lt;/dev/null 2&gt;/dev/null 
  | openssl x509 -noout -subject -issuer -dates
# 输出示例：notAfter=Mar 12 23:59:59 2026 GMT
date -u   # 与上面 notAfter 对比：系统时间超前会出现 CERT_NOT_YET_VALID</code></pre>
<p>判读规则：issuer 是公司名（如 <code>CN=Corp Proxy CA</code>）说明流量被 TLS 中间代理接管，根因是 npm 不认识该私有 CA，配 <code>cafile</code> 即可；issuer 是公共 CA（Let&#8217;s Encrypt、DigiCert 等）且 notAfter 未到，则问题在本机时间或本地 CA 库陈旧。据 Let&#8217;s Encrypt 文档，其证书有效期已缩短至 90 天以内，长期未更新的镜像里根证书过期并不罕见。</p>
<table>
<thead>
<tr>
<th>报错码</th>
<th>真实含义</th>
<th>一线定位命令</th>
<th>正确处理</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>CERT_HAS_EXPIRED</code></td>
<td>链中某张证书 notAfter 已过，或本机时间错</td>
<td><code>openssl x509 -noout -dates</code> + <code>date -u</code></td>
<td>换 CA / 校时，勿关校验</td>
</tr>
<tr>
<td><code>SELF_SIGNED_CERT_IN_CHAIN</code></td>
<td>链中出现自签根，Node 内置 CA 库不认</td>
<td><code>openssl s_client -showcerts</code></td>
<td><code>cafile</code> 或 <code>NODE_EXTRA_CA_CERTS</code></td>
</tr>
<tr>
<td><code>UNABLE_TO_GET_ISSUER_CERT_LOCALLY</code></td>
<td>CA 链不完整，缺根证书</td>
<td>同上 + <code>openssl verify</code></td>
<td><code>cafile</code> 指向根 CA</td>
</tr>
<tr>
<td><code>ENOTFOUND</code></td>
<td>DNS 解析失败，与证书无关</td>
<td><code>nslookup</code></td>
<td>查 DNS/代理</td>
</tr>
<tr>
<td><code>EAI_AGAIN</code></td>
<td>DNS 临时超时</td>
<td><code>cat /etc/resolv.conf</code></td>
<td>加重试与超时</td>
</tr>
<tr>
<td><code>CERT_NOT_YET_VALID</code></td>
<td>notBefore 在未来</td>
<td><code>timedatectl</code></td>
<td>NTP 校准</td>
</tr>
</tbody>
</table>
<p><strong>问：<code>SELF_SIGNED_CERT_IN_CHAIN</code> 能不能直接 <code>strict-ssl=false</code> 解决？</strong></p>
<p>答：能跑通，但等于关掉中间人攻击防护，npm 官方配置文档中 <code>strict-ssl</code> 默认即为 <code>true</code>，不应长期置为 <code>false</code>。正确做法是把公司根证书 PEM 加进信任链，两个入口分别覆盖 npm 自身和任意 Node 进程。</p>
<pre><code># 保存完整链，再用私有根 CA 校验（期望 Verify return code: 0 (ok)）
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org 
  -showcerts &lt;/dev/null 2&gt;/dev/null &gt; chain.pem
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org 
  -CAfile /etc/ssl/certs/corp-root-ca.pem -verify_return_error &lt;/dev/null 2&gt;&amp;1 
  | grep "Verify return code"
# cafile 只影响 npm；默认 null
npm config set cafile /etc/ssl/certs/corp-root-ca.pem
# NODE_EXTRA_CA_CERTS 影响所有 Node 程序，必须指向 PEM 文件
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem</code></pre>
<p><strong>问：<code>UNABLE_TO_GET_ISSUER_CERT_LOCALLY</code> 明明配了 cafile 还是报错？</strong></p>
<p>答：大概率把中间证书当成了 cafile。OpenSSL 只把 cafile 视作信任锚，链中间证书要用 <code>-untrusted</code> 传入。用上面的 <code>-showcerts</code> 保存完整链后，<code>cafile</code> 必须指向链末端的根证书（自签、subject 等于 issuer），中间证书交给服务端下发。</p>
<p><strong>问：<code>getaddrinfo ENOTFOUND</code> 和 <code>EAI_AGAIN</code> 也要改证书配置吗？</strong></p>
<p>答：不要。这两个错误发生在 TCP 连接建立之前，证书根本没参与握手。分开验证：<code>nslookup registry.npmjs.org 8.8.8.8</code> 确认解析，<code>curl -vI https://registry.npmjs.org</code> 确认连通，再看 <code>/etc/resolv.conf</code> 是否指向可用 DNS。<code>EAI_AGAIN</code> 属 DNS 临时失败，靠重试参数兜底，npm 中 <code>fetch-retries</code> 默认 2、<code>fetch-timeout</code> 默认 300000 毫秒，弱网或内网 DNS 慢时可放宽。</p>
<pre><code>npm config set fetch-retries 3        # 默认 2 次
npm config set fetch-timeout 60000    # 单位毫秒，默认 300000
npm config set fetch-retry-maxtimeout 120000</code></pre>
<p><strong>问：容器里反复出现 <code>CERT_NOT_YET_VALID</code> 怎么办？</strong></p>
<p>答：镜像基线漏了时区与时间同步。宿主时钟漂移、或镜像里 <code>date</code> 停在构建时刻，都会让 notBefore 落在&#8221;未来&#8221;。把校准写进 Dockerfile，而不是进容器后手工 <code>date -s</code>。</p>
<pre><code># Dockerfile 片段
ENV TZ=Asia/Shanghai
RUN apt-get update &amp;&amp; apt-get install -y chrony tzdata 
 &amp;&amp; ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
# 运行时验证
date -u; timedatectl status | grep -i synchronized</code></pre>
<p>排查时始终遵守一个原则：先确认错误发生在握手阶段还是解析阶段，再看报错码对应的 OpenSSL 校验项，最后才动配置。Mozilla 的 Server Side TLS 指南与 Node.js 官方文档对 <code>NODE_EXTRA_CA_CERTS</code> 的行为描述一致——文件在进程启动时读取一次，路径不存在仅打印警告不阻断启动，这也是&#8221;改了环境变量却没生效&#8221;的常见原因。</p>
<h2>六、效果验证方法：如何确认配置生效</h2>
<p>配置改完不等于生效——npm 的配置有项目级 <code>.npmrc</code>、用户级 <code>~/.npmrc</code>、全局 <code>$PREFIX/etc/npmrc</code>、环境变量 <code>npm_config_*</code> 四层覆盖，Node 的 <code>NODE_EXTRA_CA_CERTS</code> 又只在进程启动时读取一次。所以验证要分层做，从&#8221;配置有没有读到&#8221;一直查到&#8221;TLS 握手有没有过&#8221;。</p>
<p><strong>第一步：回读 npm 配置。</strong>据 npm 官方文档（v9/v10 一致），<code>strict-ssl</code> 默认值为 <code>true</code>，<code>cafile</code> 默认 <code>null</code>（即使用 Node 内置根证书或 <code>NODE_EXTRA_CA_CERTS</code>）。三项任一不符，先回去修 <code>.npmrc</code>，不要往下测：</p>
<pre><code>npm config get strict-ssl   # 期望输出 true
npm config get cafile       # 期望输出 /etc/ssl/certs/corp-root-ca.pem
npm config get registry     # 期望输出 https://registry.npmjs.org/
# 定位是哪一层 .npmrc 生效，避免改了用户级却被项目级覆盖
npm config list -l 2&gt;/dev/null | grep -E 'strict-ssl|cafile|registry|; "'</code></pre>
<p><strong>第二步：确认环境变量落在同一个 shell。</strong>这是最常见的&#8221;配置看着对、跑起来还是报 npm 证书过期&#8221;的原因——变量写进了 <code>~/.bashrc</code>，但 npm 跑在 systemd 或 CI 容器里，根本没继承：</p>
<pre><code>node -e 'console.log(process.env.NODE_EXTRA_CA_CERTS || "未设置")'   # 必须返回 PEM 绝对路径
# systemd 服务需显式声明，否则子进程看不到该变量
# /etc/systemd/system/xxx.service
# [Service]
# Environment=NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem
systemctl daemon-reload &amp;&amp; systemctl restart xxx</code></pre>
<p><strong>第三步：网络层验证。</strong>用 <code>--loglevel=verbose</code> 让 npm 打印真实请求，成功标志是出现 <code>http fetch GET 200 https://registry.npmjs.org/npm</code>；失败则日志会直接给出 <code>CERT_HAS_EXPIRED</code>（服务端证书确实过期）或 <code>SELF_SIGNED_CERT_IN_CHAIN</code>（企业中间人代理签发，链未受信），二者修复方向不同：</p>
<pre><code>npm view npm version --loglevel=verbose 2&gt;&amp;1 | grep -E 'http fetch|CERT_HAS_EXPIRED|SELF_SIGNED'</code></pre>
<p><strong>第四步：用 OpenSSL 单独验链</strong>，绕开 npm，判断问题出在证书链还是 npm 配置：</p>
<pre><code>openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org 
  -CAfile /etc/ssl/certs/corp-root-ca.pem &lt;/dev/null 2&gt;/dev/null | grep 'Verify return code'
# 期望：Verify return code: 0 (ok)
# 返回 20 (unable to get local issuer certificate) 或 21 (unable to verify the first certificate)
# 说明根证书或中间证书仍缺失，需把完整链写入 PEM</code></pre>
<p><strong>第五步：项目级与 CI 验证。</strong>在项目目录执行 <code>npm install --dry-run</code>，只走元数据与解析、不写 <code>node_modules</code>；CI 中先用 <code>npm ci --ignore-scripts</code> 隔离网络层，通过后再放开生命周期脚本，可快速区分&#8221;网络失败&#8221;和&#8221;脚本失败&#8221;。</p>
<table>
<thead>
<tr>
<th>验证层</th>
<th>命令</th>
<th>期望结果</th>
<th>失败指向</th>
</tr>
</thead>
<tbody>
<tr>
<td>配置</td>
<td><code>npm config get cafile</code></td>
<td>目标 PEM 路径</td>
<td><code>.npmrc</code> 层级被覆盖</td>
</tr>
<tr>
<td>环境变量</td>
<td><code>node -e ...NODE_EXTRA_CA_CERTS</code></td>
<td>PEM 路径</td>
<td>服务/容器未继承变量</td>
</tr>
<tr>
<td>网络</td>
<td><code>npm view npm version --loglevel=verbose</code></td>
<td><code>GET 200</code></td>
<td>证书过期或代理拦截</td>
</tr>
<tr>
<td>TLS 链</td>
<td><code>openssl s_client -CAfile ...</code></td>
<td><code>code 0 (ok)</code></td>
<td>返回 20/21，链不完整</td>
</tr>
</tbody>
</table>
<blockquote><p>留痕建议：<code>.npmrc</code> 不要提交含密码的 <code>proxy</code> 行（改用 <code>NPM_TOKEN</code>/环境变量注入）；把 CA 文件路径、<code>node -v</code> / <code>npm -v</code> 输出、上述四条验证命令写进 runbook，下次同类故障可在 5 分钟内复现定位。</p></blockquote>
<h2>七、延伸与预防：把 npm 证书过期纳入证书生命周期管理</h2>
<p><strong>每月怎么确认 registry.npmjs.org 的证书还没过期？</strong></p>
<p>据 Let&#8217;s Encrypt 与公共 CA 的实践，站点证书轮换通常会提前数周发布公告。建议把 <code>registry.npmjs.org</code> 纳入证书到期监控，每月巡检一次；当 <code>notAfter</code> 进入 30 天窗口时，立即查公共 CA 轮换公告：</p>
<pre><code># 适用 OpenSSL 1.1.1 / 3.x；&lt;/dev/null 防止交互式挂起
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org &lt;/dev/null 
  | openssl x509 -noout -enddate
# 输出示例：notAfter=Sep 30 23:59:59 2025 GMT
</code></pre>
<p><strong>容器镜像里 CA 证书怎么补？</strong></p>
<p>官方 Node 镜像默认带 <code>ca-certificates</code>，但企业代理根证书不会自动进入信任库；在 18.20.4、20.15.1、22.4.1 等基线镜像中显式安装并刷新：</p>
<pre><code># Debian/Ubuntu 系：node:18.20.4-bookworm-slim、20.15.1、22.4.1 均适用
RUN apt-get update 
 &amp;&amp; apt-get install -y --no-install-recommends ca-certificates 
 &amp;&amp; update-ca-certificates 
 &amp;&amp; rm -rf /var/lib/apt/lists/*
# Alpine 系改用：
# RUN apk add --no-cache ca-certificates
</code></pre>
<p><strong>根证书、代理证书和站点证书的有效期差多少？</strong></p>
<table>
<tr>
<th>对象</th>
<th>典型有效期</th>
<th>查看命令</th>
<th>更新提前量</th>
</tr>
<tr>
<td>公共根 CA</td>
<td>10–20 年</td>
<td><code>openssl x509 -in root.pem -noout -dates</code></td>
<td>60 天评估</td>
</tr>
<tr>
<td>企业代理中间证书</td>
<td>1–3 年</td>
<td><code>openssl x509 -in proxy.pem -noout -dates</code></td>
<td>60 天</td>
</tr>
<tr>
<td>registry.npmjs.org 站点证书</td>
<td>≤ 398 天（CA/B Forum 规则，持续缩短）</td>
<td>上方 <code>s_client</code> 命令</td>
<td>30 天</td>
</tr>
</table>
<p>根证书常见 10–20 年，代理中间证书往往只有 1–3 年；把两者分开管理，到期前 60 天更新 <code>.npmrc</code> 的 <code>cafile</code> 与 <code>NODE_EXTRA_CA_CERTS</code>。注意 <code>NODE_EXTRA_CA_CERTS</code> 只在进程启动时读取，更新后需重启 CI Runner 或容器，不要等 npm 证书过期报错才处理。</p>
<p><strong>怎么审计 .npmrc，防止有人关掉 strict-ssl？</strong></p>
<pre><code>npm config list -l | grep -E 'cafile|proxy|strict-ssl|registry'
# strict-ssl 默认 true；CI 门禁示例：
test "$(npm config get strict-ssl)" = "true" || { echo "strict-ssl 被关闭"; exit 1; }
</code></pre>
<p>项目仓库禁止写入 <code>strict-ssl=false</code>，它只适合本地临时定位。典型报错是 <code>npm ERR! code CERT_HAS_EXPIRED</code>，根因多为信任链中的证书到期，而非 npm 自身。</p>
<p><strong>OpenSSL 排查三件套怎么固化到值班手册？</strong></p>
<pre><code># 据 OpenSSL 官方文档：-showcerts 打印完整链，-CAfile 指定信任锚，
# -verify_return_error 让校验失败立即退出
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org 
  -showcerts -CAfile /etc/ssl/certs/ca-certificates.crt -verify_return_error &lt;/dev/null
</code></pre>
<p>把上述命令与巡检脚本写进值班手册。npm 证书过期本质是 TLS 信任链问题，可自然延伸到 OpenSSL 证书链、OCSP Stapling、Let&#8217;s Encrypt 续期、企业 CA 分发等主题，作为后续内链集群的入口。</p>
<h2>八、一页式运维清单（可复制）</h2>
<p>把前面几节的排查动作压缩成一张可打印的清单：先跑五条命令定位故障区间，再按 issuer/notAfter 二选一分支处置，最后用三条命令验收。据 npm 官方 <em>cli/config</em> 文档，<code>strict-ssl</code> 默认值为 <code>true</code>、<code>cafile</code> 默认值为 <code>null</code>（即使用 Node 内置 CA 包），这两个默认值决定了下面每条分支的起点。</p>
<p><strong>第 0 步：五连发（约 30 秒）</strong></p>
<pre><code># 依次执行，把输出整段贴进工单
node -v                      # 期望 v18.20.4+ / v20.15.1+ / v22.4.1+（含更新后的 OpenSSL 根证书库）
npm -v                       # 期望 10.8.2+
npm config get strict-ssl    # 默认 true；为 false 说明有人曾绕过
npm config get cafile        # 默认 null；非空说明被指向了企业 CA
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org -showcerts &lt;/dev/null 2&gt;/dev/null 
  | grep -E "^i:|^s:|notAfter"   # 看 issuer / subject / 到期时间三行
</code></pre>
<p><strong>第 1 步：按输出分诊</strong></p>
<table>
<tr>
<th>判据</th>
<th>结论</th>
<th>动作</th>
</tr>
<tr>
<td><code>i:</code> 行含公司名（如 <code>C=CN, O=YourCorp</code>）</td>
<td>企业 TLS 中间人代理，Node 不信任该根</td>
<td>分支 A</td>
</tr>
<tr>
<td><code>notAfter</code> 早于当前 UTC 时间，且 issuer 为公共 CA</td>
<td>真实证书过期或本机时钟漂移</td>
<td>分支 B</td>
</tr>
<tr>
<td>issuer 正常、时间正常，仅 CI 报 <code>CERT_HAS_EXPIRED</code></td>
<td>缓存或旧镜像中的旧 CA 包</td>
<td>分支 B 的升级段 + 清缓存</td>
</tr>
</table>
<p><strong>分支 A：issuer 含公司名</strong></p>
<pre><code># 1) 取证书链第 2 张（企业中间/根 CA）存为 PEM
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org -showcerts &lt;/dev/null 2&gt;/dev/null 
  | awk '/BEGIN CERT/{n++} n==2' &gt; corp-root.pem

# 2) 追加模式优先（不破坏公共 CA），cafile 是替换模式，需含完整链
export NODE_EXTRA_CA_CERTS="$PWD/corp-root.pem"   # Node 默认未定义，仅进程启动时读取一次
# 或：npm config set cafile "$PWD/corp-root.pem"

npm cache clean --force        # 清掉带旧证书元数据的缓存条目
</code></pre>
<p><strong>分支 B：公共 CA 已过期</strong>先校时——<code>date -u</code> 与真实 UTC 偏差超过 5 分钟就足以触发 <code>notAfter</code> 误判，可执行 <code>sudo timedatectl set-ntp true</code>。仍失败则升级运行时：<code>nvm install 20.15.1 &amp;&amp; nvm use 20.15.1</code>、<code>npm i -g npm@10.8.2</code>，这两个版本按 Node 官方 CHANGELOG 更新了内置 CA 包，能消掉大量历史遗留的 npm 证书过期报错。</p>
<p><strong>分支 C：必须临时绕过</strong></p>
<pre><code>npm config set strict-ssl false   # 仅用于救火，窗口 ≤10 分钟，变更单记录工单号
npm config delete strict-ssl      # 完成后立即恢复默认 true
</code></pre>
<p><strong>第 2 步：验收（三条全绿才算闭环）</strong></p>
<pre><code>npm view npm version --loglevel=verbose   # 期望看到 HTTP 200 与 registry 响应头
openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org 
  -CAfile corp-root.pem &lt;/dev/null | grep "Verify return code"   # 期望 0 (ok)
npm ci                                    # 在 CI 复现，确认非本机环境特例
</code></pre>
<p>注意 <code>Verify return code: 0 (ok)</code> 的语义遵循 OpenSSL <em>verify(1)</em> 文档：它只表示链校验通过，不校验主机名——主机名由 <code>-servername</code> 与 Node 的 <code>checkServerIdentity</code> 负责，这也是为什么清单里始终保留 <code>-servername registry.npmjs.org</code> 而不能省略。</p>
<div class="sg-faq" data-sg-faq><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">npm 证书过期是 registry.npmjs.org 的证书真的过期了吗？</button></h3><div class="sg-faq__a"><p>多数不是。先用 `openssl s_client -connect registry.npmjs.org:443 -servername registry.npmjs.org -showcerts</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">`npm config set strict-ssl false` 能永久解决 npm 证书过期吗？</button></h3><div class="sg-faq__a"><p>不能。它只是关闭 TLS 证书校验，默认值是 true；长期关闭会让中间人攻击不可见。建议只做 10 分钟隔离验证，随后执行 `npm config delete strict-ssl` 或 `npm config set strict-ssl true`。</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">企业代理下用 cafile 还是 NODE_EXTRA_CA_CERTS？</button></h3><div class="sg-faq__a"><p>单 Node 进程或 CI 优先 `NODE_EXTRA_CA_CERTS=/path/corp-root-ca.pem`；需要 npm 全局生效时用 `npm config set cafile /path/corp-root-ca.pem`。两者都只追加根证书，不要指向叶子证书，也不要重复混用多个 PEM。</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">报 `SELF_SIGNED_CERT_IN_CHAIN` 和 `CERT_HAS_EXPIRED` 有什么区别？</button></h3><div class="sg-faq__a"><p>`SELF_SIGNED_CERT_IN_CHAIN` 表示链中有自签根证书未被信任；`CERT_HAS_EXPIRED` 表示证书有效期校验失败，可能由系统时间偏差、旧 CA 包或代理证书到期引起。两者都先用 `openssl s_client -showcerts` 看 issuer 和 notAfter。</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">升级到 Node 20.15.1 后仍报 CERT_HAS_EXPIRED 怎么办？</button></h3><div class="sg-faq__a"><p>检查环境变量是否在运行 npm 的同一 shell 生效：`node -e &#039;console.log(process.env.NODE_EXTRA_CA_CERTS)&#039;`；再跑 `npm config list -l | grep -E &#039;cafile|proxy|strict-ssl&#039;`。若 issuer 仍是公司代理 CA，需要导入企业根证书，而不是继续升级 Node。</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">pnpm、yarn 也会遇到 npm 证书过期吗？</button></h3><div class="sg-faq__a"><p>会。pnpm 10 与 yarn 1 都走 Node 的 TLS 栈，根因相同；pnpm 可用 `pnpm config set cafile /path/corp-root-ca.pem`，yarn 1 可用 `yarn config set cafile /path/corp-root-ca.pem`。对比 npm 10.8.2，差异主要在配置命令，不在证书校验逻辑。</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">Docker 容器里如何一次性修复 npm 证书过期？</button></h3><div class="sg-faq__a"><p>Debian 镜像复制根证书到 `/usr/local/share/ca-certificates/corp-root-ca.crt`，执行 `update-ca-certificates`，再设置 `ENV NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corp-root-ca.pem`；Alpine 先 `apk add &#8211;no-cache ca-certificates`。构建后用 `npm view npm version &#8211;loglevel=verbose` 验证 200。</p>
</div></div></div>
<hr>
<p><em>本文由 sslgood.com 安全团队基于生产环境实战经验撰写，关键结论均经过技术复核。</em></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
