UNPKG

4.82 kBMarkdownView Raw
1# node-fetch-native
2
3[![][npm-version-src]][npm-version-href]
4[![][github-actions-src]][github-actions-href]
5[![][packagephobia-src]][packagephobia-href]
6<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
7<!-- [![Codecov][codecov-src]][codecov-href] -->
8
9A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) for better backward and forward compatibility.
10
11**Why this package?**
12
13- 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`.
14- 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.
15
16**Features:**
17
18✅ Prefer to **native globals** when available (See Node.js [experimental fetch](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch)).
19
20✅ Compact build and less install size with **zero dependencies** [![][packagephobia-s-src]][packagephobia-s-href] <sup>vs</sup> [![][packagephobia-s-alt-src]][packagephobia-s-alt-href]
21
22✅ Support both **CommonJS** (`require`) and **ESM** (`import`) usage
23
24✅ Use native version if imported without `node` condition using [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) with **zero bundle overhead**
25
26✅ Polyfill support for Node.js
27
28## Usage
29
30Install `node-fetch-native` dependency:
31
32```sh
33# npm
34npm i node-fetch-native
35
36# yarn
37yarn add node-fetch-native
38
39# pnpm
40pnpm i node-fetch-native
41```
42
43You can now either import or require the dependency:
44
45```js
46// ESM
47import fetch from 'node-fetch-native'
48
49// CommonJS
50const fetch = require('node-fetch-native')
51```
52
53More named exports:
54
55```js
56// ESM
57import { fetch, Blob, FormData, Headers, Request, Response, AbortController } from 'node-fetch-native'
58
59// CommonJS
60const { fetch, Blob, FormData, Headers, Request, Response, AbortController } = require('node-fetch-native')
61```
62
63## Force using non-native version
64
65Sometimes you want to explicitly use none native (`node-fetch`) implementation of `fetch` in case of issues with native/polyfill version of `globalThis.fetch` with Node.js or runtime environment.
66
67You have two ways to do this:
68
69- Set `FORCE_NODE_FETCH` environment variable before starting application.
70- Import from `node-fetch-native/node`
71
72## Polyfill support
73
74Using the polyfill method, we can once ensure global fetch is available in the environment and all files. Natives are always preferred.
75
76**Note:** I don't recommand this if you are authoring a library! Please prefer explicit methods.
77
78```js
79// ESM
80import 'node-fetch-native/polyfill'
81
82// CJS
83require('node-fetch-native/polyfill')
84
85// You can now use fetch() without any import!
86```
87
88## Alias to `node-fetch`
89
90Using 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`.
91
92### npm
93
94Using npm [overrides](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides):
95
96```jsonc
97// package.json
98{
99 "overrides": {
100 "node-fetch": "npm:node-fetch-native@latest"
101 }
102}
103```
104
105### yarn
106
107Using yarn [selective dependency resolutions](https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/):
108
109```jsonc
110// package.json
111{
112 "resolutions": {
113 "node-fetch": "npm:node-fetch-native@latest"
114 }
115}
116```
117
118### pnpm
119
120Using [pnpm.overrides](https://pnpm.io/package_json#pnpmoverrides):
121
122```jsonc
123// package.json
124{
125 "pnpm": {
126 "overrides": {
127 "node-fetch": "npm:node-fetch-native@latest"
128 }
129 }
130}
131```
132
133## License
134
135Made with 💛
136
137[node-fetch is published under the MIT license](https://github.com/node-fetch/node-fetch/blob/main/LICENSE.md)
138
139<!-- Badges -->
140[npm-version-src]: https://flat.badgen.net/npm/v/node-fetch-native
141[npm-version-href]: https://npmjs.com/package/node-fetch-native
142
143[npm-downloads-src]: https://flat.badgen.net/npm/dm/node-fetch-native
144[npm-downloads-href]: https://npmjs.com/package/node-fetch-native
145
146[github-actions-src]: https://flat.badgen.net/github/checks/unjs/node-fetch-native
147[github-actions-href]: https://github.com/unjs/node-fetch-native/actions?query=workflow%3Aci
148
149[packagephobia-src]: https://flat.badgen.net/packagephobia/install/node-fetch-native
150[packagephobia-href]: https://packagephobia.com/result?p=node-fetch-native
151
152[packagephobia-s-src]: https://flat.badgen.net/packagephobia/install/node-fetch-native?label=node-fetch-native&scale=.9
153[packagephobia-s-href]: https://packagephobia.com/result?p=node-fetch-native
154
155[packagephobia-s-alt-src]: https://flat.badgen.net/packagephobia/install/node-fetch?label=node-fetch&scale=.9
156[packagephobia-s-alt-href]: https://packagephobia.com/result?p=node-fetch