{"library":"node-fetch-native-with-agent","title":"Universal Node.js Fetch with Agent Support","description":"node-fetch-native-with-agent is a fork of node-fetch-native that provides a universal `fetch` implementation for Node.js, designed for compatibility across various Node.js versions and environments. It intelligently prefers the native `fetch` global (backed by Undici) when available in newer Node.js versions and falls back to a bundled `node-fetch v3` implementation for older versions, all while supporting both CommonJS (`require`) and ESM (`import`). Its key differentiator is the compact size, zero direct dependencies for the core, and robust built-in proxy support via custom agents, which is a common challenge with native `fetch` in Node.js. The current stable version is 1.7.2, with minor fixes released as needed, indicating an active maintenance cadence. It also includes polyfill capabilities to make `fetch` globally available. This package aims to solve the `node-fetch` v2 vs v3 dependency conflicts and the lack of straightforward proxy support with Node.js's native `fetch`.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-fetch-native-with-agent"],"cli":null},"imports":["import fetch from 'node-fetch-native-with-agent';","import { fetch, Headers, Request } from 'node-fetch-native-with-agent';","import 'node-fetch-native-with-agent/polyfill';","import fetch from 'node-fetch-native-with-agent/node';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import fetch from 'node-fetch-native-with-agent';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\n\nasync function fetchDataWithAgent() {\n  const proxyUrl = process.env.HTTPS_PROXY || 'http://localhost:8080'; // Replace with your proxy URL\n  let agent;\n\n  // Check if a proxy URL is provided\n  if (proxyUrl) {\n    // node-fetch-native-with-agent automatically handles Undici/non-Undici agent types.\n    // For explicit agent usage, especially with older Node.js versions or specific configurations,\n    // you might pass a compatible agent instance.\n    // This example shows how you might set up an HttpsProxyAgent, which the library then uses.\n    console.log(`Using proxy: ${proxyUrl}`);\n    agent = new HttpsProxyAgent(proxyUrl);\n  } else {\n    console.log('No proxy configured. Fetching directly.');\n  }\n\n  try {\n    const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {\n      agent: agent // The library will pick up the agent automatically if env vars are set\n                   // but explicit passing is also supported for custom agents.\n    });\n    if (!response.ok) {\n      throw new Error(`HTTP error! Status: ${response.status}`);\n    }\n    const data = await response.json();\n    console.log('Fetched Data:', data);\n  } catch (error) {\n    console.error('Error fetching data:', error);\n  }\n}\n\nfetchDataWithAgent();","lang":"typescript","description":"Demonstrates basic `fetch` usage with `node-fetch-native-with-agent`, including how to configure and use an `HttpsProxyAgent` for HTTP/S proxy support. It explicitly shows agent creation, although the library also respects `HTTP_PROXY`/`HTTPS_PROXY` environment variables automatically.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}