- Notifications
You must be signed in to change notification settings - Fork5
better fetch for Node.js. Works on any JavaScript runtime!
License
unjs/node-fetch-native
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A redistribution ofnode-fetch v3 (+ more!) for better backward and forward compatibility.
Why this package?
- We can no longer
require('node-fetch')
with the latest version. This stopped popular libraries from upgrading and dependency conflicts betweennode-fetch@2
andnode-fetch@3
. - 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. - With the introduction of native fetch to Node.js viaundici there is no easy way to support http proxies!
Features:
✅ Prefer tonative globals when available (See Node.jsexperimental fetch).
✅ Compact build and less install size withzero dependenciesvs
✅ Support bothCommonJS (require
) andESM (import
) usage
✅ Use native version if imported withoutnode
condition usingconditional exports withzero bundle overhead
✅ Polyfill support for Node.js
✅ Compact and simple proxy supporting both Node.js versions without native fetch usingHTTP Agent and versions with native fetch usingUndici Proxy Agent
Installnode-fetch-native
dependency:
# npmnpm i node-fetch-native# yarnyarn add node-fetch-native# pnpmpnpm i node-fetch-native
You can now either import or require the dependency:
// ESMimportfetchfrom"node-fetch-native";// CommonJSconstfetch=require("node-fetch-native");
More named exports:
// ESMimport{fetch,Blob,FormData,Headers,Request,Response,AbortController,}from"node-fetch-native";// CommonJSconst{ fetch, Blob, FormData, Headers, Request, Response, AbortController,}=require("node-fetch-native");
Sometimes you want to explicitly use none native (node-fetch
) implementation offetch
in case of issues with the native/polyfill version ofglobalThis.fetch
with Node.js or runtime environment.
You have two ways to do this:
- Set the
FORCE_NODE_FETCH
environment variable before starting the application. - Import from
node-fetch-native/node
Once thenode-fetch-native/node
module is loaded, it pushes a log warning if the current runtime differs from the Node.js. Set theDISABLE_NODE_FETCH_NATIVE_WARN
environment variable to turn this check off.
Using the polyfill method, we can ensure global fetch is available in the environment and all files. Natives are always preferred.
Note: I don't recommend this if you are authoring a library! Please prefer the explicit methods.
// ESMimport"node-fetch-native/polyfill";// CJSrequire("node-fetch-native/polyfill");// You can now use fetch() without any import!
Node.js has no built-in support for HTTP Proxies for fetch (seenodejs/undici#1650 andnodejs/node#8381)
This package bundles a compact and simple proxy-supported solution for both Node.js versions without native fetch usingHTTP Agent and versions with native fetch usingUndici Proxy Agent.
By default,https_proxy
,http_proxy
,HTTPS_PROXY
, andHTTP_PROXY
environment variables will be checked and used (in order) for the proxy and if not any of them are set, the proxy will be disabled. You can override it using theurl
option passed tocreateFetch
andcreateProxy
utils.
By default,no_proxy
andNO_PROXY
environment variables will be checked and used for the (comma-separated) list of hosts to ignore the proxy for. You can override it using thenoProxy
option passed tocreateFetch
andcreateProxy
utils. The entries starting with a dot will be used to check the domain and also any subdomain.
Note
Using export conditions, this utility adds proxy support for Node.js and for other runtimes, it will simply return native fetch.
You can simply import{ fetch }
fromnode-fetch-native/proxy
with a preconfiguredfetch
function that has proxy support.
import{fetch}from"node-fetch-native/proxy";console.log(awaitfetch("https://icanhazip.com").then((r)=>r.text()));
You can use thecreateFetch
utility to instantiate afetch
instance with custom proxy options.
import{createFetch}from"node-fetch-native/proxy";constfetch=createFetch({url:"http://localhost:9080"});console.log(awaitfetch("https://icanhazip.com").then((r)=>r.text()));
createProxy
returns an object withagent
anddispatcher
keys that can be passed as fetch options.
import{fetch}from"node-fetch-native";import{createProxy}from"node-fetch-native/proxy";constproxy=createProxy();// const proxy = createProxy({ url: "http://localhost:8080" });console.log(awaitfetch("https://icanhazip.com",{ ...proxy}).then((r)=>r.text()),);
Using this method, you can ensure all project dependencies and usages ofnode-fetch
can benefit from improvednode-fetch-native
and won't conflict betweennode-fetch@2
andnode-fetch@3
.
Using npmoverrides:
// package.json{"overrides": {"node-fetch":"npm:node-fetch-native@latest", },}
Using yarnselective dependency resolutions:
// package.json{"resolutions": {"node-fetch":"npm:node-fetch-native@latest", },}
Usingpnpm.overrides:
// package.json{"pnpm": {"overrides": {"node-fetch":"npm:node-fetch-native@latest", }, },}
Made with 💛 Published under theMIT license.
About
better fetch for Node.js. Works on any JavaScript runtime!