Isomorphic Fetch API
Isomorphic-fetch is a JavaScript library that provides the WHATWG Fetch API for both Node.js and browser environments. Its current stable version is 3.0.0. The package operates as a polyfill, meaning it modifies the global scope to add the `fetch` function and related constructs like `Headers`, `Request`, and `Response`. For server-side operations, it leverages `node-fetch`, while for client-side, it's built on GitHub's WHATWG Fetch polyfill. Its release cadence has slowed, with the last major version (v3.0.0) released significantly after previous iterations, suggesting a maintenance rather than active development phase. A key differentiator is its polyfill approach, which automatically makes `fetch` available globally, contrasting with 'ponyfill' alternatives like `fetch-ponyfill` or `cross-fetch` that avoid global pollution. This design choice aims for API consistency but requires users to be aware of global modifications.
Common errors
-
TypeError: fetch is not a function
cause `isomorphic-fetch` relies on its import to set the global `fetch` object. This error often occurs if `require('isomorphic-fetch')` is not called, or not called early enough, in your application's lifecycle, or if another library overwrites the global `fetch`.fixEnsure `require('isomorphic-fetch');` is placed at the very top of your application's main entry file or within any module that intends to use the global `fetch`. Alternatively, import the `fetch` function directly using `const fetch = require('isomorphic-fetch');`. -
ReferenceError: Headers is not defined
cause While `isomorphic-fetch` polyfills `Headers` and other Fetch API primitives globally, some environments or module bundling configurations might require explicit import or different execution order for these constructors.fixVerify that `require('isomorphic-fetch');` has executed. If you need to access `Headers`, `Request`, or `Response` explicitly, import them using named CJS destructuring: `const { Headers, Request, Response } = require('isomorphic-fetch');`.
Warnings
- gotcha This package acts as a global polyfill, adding `fetch`, `Headers`, `Request`, and `Response` to the global scope. This can conflict with other polyfills or native browser implementations, leading to unexpected behavior or breakage.
- gotcha Isomorphic-fetch is a polyfill, meaning it will only add `fetch` if it's not already present. If your environment already has a `fetch` implementation (e.g., a newer Node.js version, or a modern browser), `isomorphic-fetch` might not have an effect or could cause subtle inconsistencies depending on its internal checks.
- breaking Version 2.0.0 switched its server-side implementation from the GitHub polyfill to `node-fetch`. While generally compatible, subtle differences in behavior, error handling, or stream management between the two underlying libraries could affect existing Node.js applications.
Install
-
npm install isomorphic-fetch -
yarn add isomorphic-fetch -
pnpm add isomorphic-fetch
Imports
- Global side effect
require('isomorphic-fetch'); - fetch
import { fetch } from 'isomorphic-fetch';const fetch = require('isomorphic-fetch'); - Headers, Request, Response
import { Headers, Request, Response } from 'isomorphic-fetch';const { Headers, Request, Response } = require('isomorphic-fetch');
Quickstart
require('isomorphic-fetch');
fetch('//offline-news-api.herokuapp.com/stories')
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
})
.then(function(stories) {
console.log(stories);
})
.catch(function(error) {
console.error('Fetch failed:', error);
});