fetch-h2 is a robust Node.js implementation of the standard Fetch API, providing a familiar, browser-like interface for making HTTP requests within a Node.js environment. It transparently handles both HTTP/1.1 and HTTP/2 connections, automatically negotiating the protocol via ALPN for `https://` URLs, and defaulting to HTTP/1.1 for `http://` unless `http2://` is explicitly used for plain-text HTTP/2 (h2c). The library is currently stable at version 3.0.2 and receives regular maintenance, including bug fixes and dependency updates. Key differentiators include its close adherence to the Fetch API standard, transparent socket re-use and session management, built-in in-memory cookie support per context, and automatic decoding of `br`, `gzip`, and `deflate` encodings. It offers a higher-level, more developer-friendly abstraction for HTTP/2 client requests in Node.js compared to the lower-level native `http2` module. Since version 3.0.0, it requires Node.js 12 or newer.
npm install fetch-h2Verified import paths — ran on the pinned version, not inferred.
Demonstrates a basic GET request using `fetch`, handling the response, and error conditions, including custom headers.
Upgrade your Node.js environment to version 12 or higher, or use an older version of fetch-h2 (e.g., <3.0.0) if Node 10 support is critical.
Consult the `engines` field in the package.json or the release notes for the exact Node.js version compatibility.
If persistent cookie storage or cross-process sharing is required, manually retrieve cookies from `CookieJar` and manage them outside the library.
For h2c connections, ensure your URL starts with `http2://` (e.g., `http2://localhost:8080`). For HTTPS, ALPN negotiation handles protocol selection.
Catch `TimeoutError` specifically if you need to differentiate timeout errors from other abort signals. Otherwise, catching `AbortError` will cover both.
Use ESM `import { fetch } from 'fetch-h2'` in a module with `"type": "module"` in package.json or with an `.mjs` extension.Increase the timeout value in the `init` object (e.g., `{ timeout: 10000 }`) or investigate network/server performance issues.Check the URL, verify the resource exists, and ensure the server is configured to return expected status codes. Add specific error handling for different HTTP statuses.
No dependency data recorded yet.