{"library":"node-fetch-native","title":"Node.js `fetch` Compatibility Layer","description":"node-fetch-native is a lightweight package designed to provide a consistent `fetch` API across various JavaScript runtimes, primarily targeting Node.js. It intelligently prefers Node.js's experimental native `fetch` (backed by `undici`) when available, falls back to an internal `node-fetch v3` implementation for older Node.js versions, and offers comprehensive HTTP/HTTPS proxy support, which is not natively available in Node.js's built-in `fetch` implementation. Currently stable at version `1.6.7`, the package maintains a steady release cadence, often addressing dependency updates and minor bug fixes. A key differentiator is its ability to bridge the `CommonJS` (`require`) and `ESM` (`import`) divide for `fetch`, resolving common migration issues encountered with `node-fetch` v3. It also provides a polyfill mechanism to ensure global `fetch` availability.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-fetch-native"],"cli":null},"imports":["import fetch from 'node-fetch-native';","const fetch = require('node-fetch-native');","import { fetch, Headers, Request, Response } from 'node-fetch-native';","import fetch from 'node-fetch-native/node';","import 'node-fetch-native/polyfill';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import fetch, { Headers, Request } from 'node-fetch-native';\nimport 'node-fetch-native/polyfill'; // Optional: ensures global fetch is available\n\nasync function runExample() {\n  console.log(\"--- Basic Fetch Example ---\");\n  try {\n    const response = await fetch(\"https://api.github.com/zen\");\n    const text = await response.text();\n    console.log(`GitHub Zen says: \"${text}\"`);\n  } catch (error) {\n    console.error(\"Basic fetch failed:\", error);\n  }\n\n  console.log(\"\\n--- Fetch with Named Exports Example ---\");\n  try {\n    const headers = new Headers({ \"Content-Type\": \"application/json\" });\n    const request = new Request(\"https://jsonplaceholder.typicode.com/posts\", {\n      method: \"POST\",\n      headers: headers,\n      body: JSON.stringify({ title: \"foo\", body: \"bar\", userId: 1 }),\n    });\n    const response = await fetch(request);\n    const data = await response.json();\n    console.log(\"POST request response:\", data);\n  } catch (error) {\n    console.error(\"Named exports fetch failed:\", error);\n  n}\n\n  // Example of using a proxy (requires setting environment variables like HTTP_PROXY)\n  if (process.env.HTTP_PROXY || process.env.HTTPS_PROXY) {\n    console.log(\"\\n--- Proxy-aware Fetch (if env vars are set) ---\");\n    try {\n      const response = await fetch(\"http://example.com\");\n      console.log(`Fetched http://example.com via proxy (status: ${response.status})`);\n    } catch (error) {\n      console.error(\"Proxy fetch failed:\", error);\n    }\n  } else {\n    console.log(\"\\n--- Proxy-aware Fetch: Set HTTP_PROXY or HTTPS_PROXY environment variable to test. ---\");\n    console.log(\"Example: export HTTP_PROXY=http://localhost:8080\");\n  }\n}\n\nrunExample();","lang":"typescript","description":"Demonstrates basic `fetch` usage, fetching with named `Headers` and `Request` objects, and shows how `node-fetch-native` inherently supports HTTP/HTTPS proxies when `HTTP_PROXY` or `HTTPS_PROXY` environment variables are configured. It highlights both default and named imports.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}