make-fetch-happen

15.0.5 · active · verified Tue Apr 21

make-fetch-happen is an opinionated, robust Node.js HTTP client that extends the standard `fetch` API with critical features for real-world applications. Currently stable at version 15.0.5, it maintains a fairly active release cadence, with minor/patch updates occurring every few months and major versions roughly annually, aligning with Node.js LTS updates. It distinguishes itself by wrapping `minipass-fetch` and augmenting it with capabilities like automatic HTTP-semantics-aware request retries, comprehensive HTTP caching (`Cache-Control`, `ETag`, `304`s, offline fallback), request pooling, transparent gzip/deflate, and support for proxies (HTTP, HTTPS, SOCKS). Additionally, it provides Subresource Integrity (SRI) verification and integrates Node.js Stream support, making it a highly reliable and performant choice for network operations within the Node.js ecosystem, particularly for tools like `npm` itself.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `make-fetch-happen` with a persistent cache path and perform both initial and conditional HTTP requests, showcasing its caching capabilities.

import makeFetchHappen from 'make-fetch-happen';

const fetch = makeFetchHappen.defaults({
  cachePath: './my-app-cache' // path where cache will be written (and read)
});

// First request: fetches from the web and caches the response
fetch('https://registry.npmjs.org/make-fetch-happen')
  .then(res => res.json()) // download the body as JSON
  .then(body => {
    console.log(`Initial fetch: got ${body.name} from web.`);
    // Second request: forces a conditional request to validate cache
    return fetch('https://registry.npmjs.org/make-fetch-happen', {
      cache: 'no-cache' // instructs to revalidate with the origin
    });
  })
  .then(res => {
    console.log(`Conditional fetch status: ${res.status}`); // Expected 304 if cache valid
    return res.json().then(body => {
      console.log(`Conditional fetch: got ${body.name} from cache (status ${res.status}).`);
    });
  })
  .catch(error => console.error('An error occurred:', error));

view raw JSON →