Isomorphic Unfetch is a lightweight JavaScript library that provides a universal `fetch` API implementation, automatically switching between `unfetch` (a minimal `fetch` ponyfill for browsers) and `node-fetch` (a `fetch` implementation for Node.js) based on the execution environment. The current stable version is 5.0.0. This package aims for a small footprint and consistent behavior across client-side and server-side JavaScript, abstracting away environment-specific `fetch` implementations. It provides both a ponyfill (default named import) and a global polyfill (side-effect import). Recent major updates include the adoption of `node-fetch` v3.x, which mandates Node.js >= 12.20.0, and the addition of first-class TypeScript definitions and Package Exports for improved module resolution. It focuses on simplicity and compatibility with standard `fetch` API usage.
npm install isomorphic-unfetchVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `isomorphic-unfetch` as a ponyfill to make `GET` requests, handle responses, and includes basic TypeScript typing for typical `fetch` API usage.
Upgrade your Node.js environment to version 12.20.0 or higher, or downgrade `isomorphic-unfetch` to a version prior to 4.0.1.
If you relied on `fetch` being globally available, switch your import from `import fetch from 'isomorphic-unfetch'` to `import 'isomorphic-unfetch/polyfill';` (for side effects) or explicitly assign the imported `fetch` to `globalThis.fetch`.
Ensure your build tools (e.g., webpack, rollup, TypeScript, Node.js runtime) are updated to versions that correctly handle `package.json` `exports` fields. Update TypeScript to benefit from the new definitions.
Ensure you are using `isomorphic-unfetch` version 3.1.0 or higher to include the `node-fetch` security fix.
Update to `isomorphic-unfetch@4.2.0` or higher to ensure `.json()` parse errors are handled as rejected Promises, allowing for consistent `try/catch` or `.catch()` error handling.
Upgrade Node.js to version 12.20.0 or newer. If still encountering issues, ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) or use dynamic `import('isomorphic-unfetch')`.Either import the `fetch` function explicitly (e.g., `import fetch from 'isomorphic-unfetch';`) and use it, or import the polyfill for global availability (`import 'isomorphic-unfetch/polyfill';`).
Always `await` `response.json()` or use `.then()` on it, for example: `const data = await response.json();`. Ensure the server response actually contains valid JSON.