版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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