API Reference

node-fetch

Lightweight WHATWG fetch implementation for Node.js providing the standard Request, Response, and Headers objects.

Official Documentation ↗
CompatibilityThis documentation covers version v3 (ESM only). Check the official docs for the latest API surface.

Installation

npmnpm install node-fetch
pnpmpnpm add node-fetch

Methods

Lightweight WHATWG fetch implementation for Node.js providing the standard Request, Response, and Headers objects.

Overview

node-fetch brought the browser fetch API to Node before it was native. Requests produce standard Response objects with case-insensitive Headers, body readers for text, JSON, buffers, and streams, and helper properties such as ok and statusText.

Its Node heritage shows in useful places: res.body is a real Node readable stream that pipes into filesystem or HTTP destinations, and res.buffer() returns a Buffer directly. The package is ESM-only since v3, which also marks the point where new projects could simply use the global fetch instead.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • fetch — Performs an HTTP request and resolves with a spec Response exposing status, ok, headers, and body readers.
  • body readers — Consumes the response body once in the requested shape; buffer is the Node-specific addition.
  • Headers / Response helpers — Reads header values case-insensitively and checks the 2xx shorthand before parsing payloads.
  • streaming body — The response body as a native Node readable stream for piping downloads without buffering.

Usage Notes

  • Always branch on res.ok before parsing; a JSON error page parses fine and hides the failure.
  • Use res.clone() when middleware needs to inspect the body while the handler still consumes it.
  • Set an AbortSignal timeout wrapper around fetch calls; node-fetch has no built-in request timeout.

Common Pitfalls

  • The body is consumed once; reading it in logging middleware breaks the downstream json() call.
  • Redirect handling re-sends the body by default only for safe methods; POST redirects need explicit redirect mode review.
  • CommonJS require of v3 fails: it is ESM-only, matching the ecosystem direction.

Errors

The following points are quoted verbatim from the official documentation (https://github.com/node-fetch/node-fetch). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Why use node-fetch when Node has global fetch?
Node 18+ ships a native fetch, so new code should use the global. node-fetch remains useful in older Node runtimes, in projects that need its buffer helper, or where the smaller dependency surface behaves more predictably than the built-in undici fetch.
Why does res.json() throw "body already used"?
A Response body is single-use: json, text, buffer, and arrayBuffer each consume it. When you need both a log line and parsed data, clone the response, or read text once and parse with JSON.parse.
How do I send JSON correctly?
Set headers: { "content-type": "application/json" } and pass JSON.stringify(payload) as body. node-fetch does not auto-serialize objects, and a plain object body sends "[object Object]".