Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

better fetch for Node.js. Works on any JavaScript runtime!

License

NotificationsYou must be signed in to change notification settings

unjs/node-fetch-native

Repository files navigation

npm downloads

A redistribution ofnode-fetch v3 (+ more!) for better backward and forward compatibility.

Why this package?

  • We can no longerrequire('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, nativefetch 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

Usage

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");

Force using non-native version

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 theFORCE_NODE_FETCH environment variable before starting the application.
  • Import fromnode-fetch-native/node

Disable runtime check

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.

Polyfill support

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!

Proxy Support

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.

fetch with proxy support

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()));

createFetch utility

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 utility

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()),);

Alias tonode-fetch

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.

npm

Using npmoverrides:

// package.json{"overrides": {"node-fetch":"npm:node-fetch-native@latest",  },}

yarn

Using yarnselective dependency resolutions:

// package.json{"resolutions": {"node-fetch":"npm:node-fetch-native@latest",  },}

pnpm

Usingpnpm.overrides:

// package.json{"pnpm": {"overrides": {"node-fetch":"npm:node-fetch-native@latest",    },  },}

License

Made with 💛 Published under theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp