### Install node-fetch-native Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Install the node-fetch-native package using npm, yarn, or pnpm. ```sh # npm npm i node-fetch-native # yarn yarn add node-fetch-native # pnpm pnpm i node-fetch-native ``` -------------------------------- ### Fetch with Proxy Support Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Import a preconfigured fetch function with proxy support directly from node-fetch-native/proxy. This simplifies setting up fetch for environments requiring proxy configurations. ```TypeScript import { fetch } from "node-fetch-native/proxy"; console.log(await fetch("https://icanhazip.com").then((r) => r.text())); ``` -------------------------------- ### Create Fetch Instance with Custom Proxy Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Utilize the createFetch utility to instantiate a custom fetch instance with specific proxy options. This allows for granular control over proxy settings for different fetch operations. ```TypeScript import { createFetch } from "node-fetch-native/proxy"; const fetch = createFetch({ url: "http://localhost:9080" }); console.log(await fetch("https://icanhazip.com").then((r) => r.text())); ``` -------------------------------- ### Create Proxy Agent/Dispatcher Source: https://github.com/unjs/node-fetch-native/blob/main/README.md The createProxy utility generates an object containing agent and dispatcher properties. These can be spread into fetch options to apply proxy configurations to standard fetch calls. ```TypeScript import { fetch } from "node-fetch-native"; import { createProxy } from "node-fetch-native/proxy"; const proxy = createProxy(); // const proxy = createProxy({ url: "http://localhost:8080" }); console.log( await fetch("https://icanhazip.com", { ...proxy }).then((r) => r.text()), ); ``` -------------------------------- ### Polyfill Global Fetch Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Ensure the global fetch API is available in all files by importing the polyfill. This method is generally recommended for applications, not libraries. Natives are always preferred. ```javascript // ESM import "node-fetch-native/polyfill"; // CJS require("node-fetch-native/polyfill"); // After importing, fetch() is available globally without explicit import. ``` -------------------------------- ### Basic Usage: Import/Require Fetch Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Import or require the fetch function from node-fetch-native for use in your project. Supports both ESM and CommonJS module systems. ```javascript // ESM import fetch from "node-fetch-native"; // CommonJS const fetch = require("node-fetch-native"); ``` -------------------------------- ### Alias node-fetch with npm Overrides Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Configure npm overrides in package.json to alias the 'node-fetch' dependency to 'node-fetch-native'. This ensures all project usages of node-fetch benefit from node-fetch-native's features and compatibility. ```JSON // package.json { "overrides": { "node-fetch": "npm:node-fetch-native@latest" } } ``` -------------------------------- ### Alias node-fetch with pnpm Overrides Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Configure pnpm.overrides in package.json to alias 'node-fetch' to 'node-fetch-native'. This is the recommended approach for pnpm users to manage dependency aliasing and ensure compatibility. ```JSON // package.json { "pnpm": { "overrides": { "node-fetch": "npm:node-fetch-native@latest" } } } ``` -------------------------------- ### Force Non-Native Fetch Implementation Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Explicitly use the non-native (node-fetch) implementation by setting the FORCE_NODE_FETCH environment variable or importing from 'node-fetch-native/node'. This is useful for compatibility or debugging. ```javascript // Set environment variable before starting application: // FORCE_NODE_FETCH=true node your_app.js // Or import from a specific path: // import fetch from "node-fetch-native/node"; // ESM // const fetch = require("node-fetch-native/node"); // CommonJS ``` -------------------------------- ### Alias node-fetch with Yarn Resolutions Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Use Yarn's selective dependency resolutions in package.json to alias 'node-fetch' to 'node-fetch-native'. This method is effective for managing dependency versions and ensuring consistent fetch behavior. ```JSON // package.json { "resolutions": { "node-fetch": "npm:node-fetch-native@latest" } } ``` -------------------------------- ### Disable Runtime Warning Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Prevent the package from logging a warning if the current runtime environment differs from Node.js. Set the DISABLE_NODE_FETCH_NATIVE_WARN environment variable to true. ```javascript // Set environment variable before starting application: // DISABLE_NODE_FETCH_NATIVE_WARN=true node your_app.js ``` -------------------------------- ### Named Exports Source: https://github.com/unjs/node-fetch-native/blob/main/README.md Access other components like Blob, FormData, Headers, Request, Response, and AbortController by importing them as named exports. This applies to both ESM and CommonJS. ```javascript // ESM import { fetch, Blob, FormData, Headers, Request, Response, AbortController, } from "node-fetch-native"; // CommonJS const { fetch, Blob, FormData, Headers, Request, Response, AbortController, } = require("node-fetch-native"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.