You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何JSON.parse未报错却忽略数据?npm包接口readme字段异常排查

Why is the readme field empty when parsing npm registry JSON with fetch?

Great question! Let me break down exactly what's going on here:

The Root Cause

The npm registry API serves different versions of package metadata based on the Accept header included in your request:

  • When you load the URL directly in your browser, your browser sends an Accept header that includes values like text/html or application/json. The registry responds with the full package metadata, including the complete readme content you see.
  • When you use a default fetch() call, the browser sends a broader Accept header (typically */*). The npm registry interprets this as a request for the minified, install-focused version of the metadata—this is the format npm uses internally when installing packages, so non-essential fields like readme are emptied to reduce payload size.

How to Fix It

To retrieve the full metadata (including the complete readme), explicitly set the Accept header to application/json in your fetch request:

fetch('http://registry.npmjs.org/core-js', {
  headers: {
    'Accept': 'application/json'
  }
})
.then(response => response.json())
.then(data => console.log(data.readme)); // Now you'll see the full README content!

Quick Way to Verify

You can confirm this behavior using your browser's dev tools:

  1. Load the URL directly and check the Accept request header in the Network tab—you'll see the full readme in the response.
  2. Run your original fetch code, inspect the request headers, and notice the response has an empty readme field.
  3. Run the modified fetch code with the explicit Accept header, and you'll see the full readme loads correctly.

内容的提问来源于stack exchange,提问作者Juan Picado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:25:32