|
| 1 | +# node-fetch-native |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | +[![Github Actions][github-actions-src]][github-actions-href] |
| 6 | +<!-- [![Codecov][codecov-src]][codecov-href] --> |
| 7 | + |
| 8 | +A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) for better backward and forward compatibility. |
| 9 | + |
| 10 | +Why this package? |
| 11 | + |
| 12 | +- We can no longer `require('node-fetch')` with latest version. This stopped popular libraries from upgrading and dependency conflicts between `node-fetch@2` and `node-fetch@3`. |
| 13 | +- With upcoming versions of Node.js, native `fetch` is being supported. We are prepared for native fetch support using this package yet keep supporting older Node versions. |
| 14 | + |
| 15 | +✅ Prefer to **native globals** when available (`fetch`, `Blob`, `File`, `FormData`, `Headers`, `Request`, and `Response`) when available (Node.js [experimental fetch](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch)) |
| 16 | + |
| 17 | +✅ Compact build and less install size with **zero dependencies** |
| 18 | + |
| 19 | +✅ Support both **CommonJS** (`require`) and **ESM** (`import`) usage |
| 20 | + |
| 21 | +✅ Use native version if imported without `node` condition using [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) with **zero bundle overhead** |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +Install `node-fetch-native` dependency: |
| 26 | + |
| 27 | +```sh |
| 28 | +# npm |
| 29 | +npm i node-fetch-native |
| 30 | + |
| 31 | +# yarn |
| 32 | +yarn add node-fetch-native |
| 33 | + |
| 34 | +# pnpm |
| 35 | +pnpm i node-fetch-native |
| 36 | +``` |
| 37 | + |
| 38 | +You can now either import or require the dependency: |
| 39 | + |
| 40 | +```js |
| 41 | +// ESM |
| 42 | +import fetch from `node-fetch-native` |
| 43 | + |
| 44 | +// CommonJS |
| 45 | +const fetch = require('node-fetch-native') |
| 46 | +``` |
| 47 | + |
| 48 | +Other exports: |
| 49 | + |
| 50 | +```js |
| 51 | +// ESM |
| 52 | +import { fetch, Blob, FormData, Headers, Request, Response } from `node-fetch-native` |
| 53 | + |
| 54 | +// CommonJS |
| 55 | +const { fetch, Blob, FormData, Headers, Request, Response } = require('node-fetch-native') |
| 56 | +``` |
| 57 | + |
| 58 | +## Alias to `node-fetch` |
| 59 | + |
| 60 | +Using this method, you can ensure all project dependencies and usages of `node-fetch` can benefit from improved `node-fetch-native` and won't conflict between `node-fetch@2` and `node-fetch@3`. |
| 61 | + |
| 62 | +### npm |
| 63 | + |
| 64 | +Using npm [overrides](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides): |
| 65 | + |
| 66 | +```jsonc |
| 67 | +// package.json |
| 68 | +{ |
| 69 | + "overrides": { |
| 70 | + "node-fetch": "npm:node-fetch-native@latest" |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### yarn |
| 76 | + |
| 77 | +Using yarn [selective dependency resolutions](https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/): |
| 78 | + |
| 79 | +```jsonc |
| 80 | +// package.json |
| 81 | +{ |
| 82 | + "resolutions": { |
| 83 | + "node-fetch": "npm:node-fetch-native@latest" |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### pnpm |
| 89 | + |
| 90 | +Using [pnpm.overrides](https://pnpm.io/package_json#pnpmoverrides): |
| 91 | + |
| 92 | +```jsonc |
| 93 | +// package.json |
| 94 | +{ |
| 95 | + "pnpm": { |
| 96 | + "overrides": { |
| 97 | + "node-fetch": "npm:node-fetch-native@latest" |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +Made with 💛 |
| 106 | + |
| 107 | +[node-fetch is published under the MIT license](https://github.com/node-fetch/node-fetch/blob/main/LICENSE.md) |
| 108 | + |
| 109 | +<!-- Badges --> |
| 110 | +[npm-version-src]: https://img.shields.io/npm/v/node-fetch-native?style=flat-square |
| 111 | +[npm-version-href]: https://npmjs.com/package/node-fetch-native |
| 112 | + |
| 113 | +[npm-downloads-src]: https://img.shields.io/npm/dm/node-fetch-native?style=flat-square |
| 114 | +[npm-downloads-href]: https://npmjs.com/package/node-fetch-native |
| 115 | + |
| 116 | +[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/node-fetch-native/ci/main?style=flat-square |
| 117 | +[github-actions-href]: https://github.com/unjs/node-fetch-native/actions?query=workflow%3Aci |
| 118 | + |
| 119 | +<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/node-fetch-native/main?style=flat-square |
| 120 | +[codecov-href]: https://codecov.io/gh/unjs/node-fetch-native --> |
0 commit comments