# native-fetch

> Returns native fetch if available or the undici module if not

Latest version **4.0.2** (published 2021-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install native-fetch
pnpm add native-fetch
yarn add native-fetch
bun add native-fetch
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2021-12-13 |
| First published | 2018-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Alex Potsides |
| Maintainers | achingbrain |

## Links

- npm: https://www.npmjs.com/package/native-fetch
- Repository: https://github.com/achingbrain/native-fetch
- Homepage: https://github.com/achingbrain/native-fetch#readme
- Issues: https://github.com/achingbrain/native-fetch/issues
- npm.io page: https://npm.io/package/native-fetch

## Recent versions

- 4.0.2 (latest) — 2021-12-13
- 4.0.1 — 2021-12-13
- 4.0.0 — 2021-11-17
- 3.0.0 — 2021-02-03
- 2.0.1 — 2020-10-06
- 2.0.0 — 2020-09-03
- 1.0.0 — 2018-11-13
- 0.0.0 — 2018-11-13

## README

# native-fetch

> Returns native fetch/Request/Headers if available or the `undici` module if not

An (almost) drop-in replacement for the `undici` module that returns the native fetch if available or the polyfill if not.

### Why?

Some environments such as the Electron Renderer process straddle the node/browser divide with features from both APIs available.  In these cases the webpack approach of always using the `browser` field in your `package.json` to override requires is too heavy-handed as sometimes you want to use the node version of an API.

Instead we can check for the availability of a given API and return it, rather than the node-polyfill for that API.

### Why Undici and not node-fetch?

[node-fetch](https://www.npmjs.com/package/node-fetch) is the OG fetch implementation for node, but it uses [Node.js streams](https://nodejs.org/api/stream.html) instead of [WHATWG streams](https://streams.spec.whatwg.org/). This means the APIs are not the same which leads to all sorts of weird shenanigans with types.

[undici](https://www.npmjs.com/package/undici) is the new kid on the block and uses WHATWG streams so all of the APIs now live in glorious harmony.

If you are trying to write polymorphic code with strong typing this is a big deal.

## Install

You must install a version of `undici` [alongside this module](https://docs.npmjs.com/files/package.json#peerdependencies) to be used if a native implementation is not available.

```console
$ npm install --save native-fetch undici
```

## Usage

```javascript
const { fetch } = require('native-fetch')

fetch('https://github.com/')
    .then(res => res.text())
    .then(body => console.log(body))
```

---
_Source: https://npm.io/package/native-fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
