A critical detail for developers is the versioning change. Starting with , node-fetch transitioned to an ES Module (ESM) only package .
Fetching data is straightforward. You call fetch(url) , which returns a promise that resolves to a Response object. javascript node fetch
import fetch from 'node-fetch'; async function getData() { try { const response = await fetch('https://example.com'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // Parses the JSON body console.log(data); } catch (error) { console.error('Fetch failed:', error); } } Use code with caution. 2. Sending Data with POST A critical detail for developers is the versioning change
As of , the Fetch API is now built-in and stable . This means for modern applications running on recent Node versions, you can use fetch() globally without installing any external packages. Why still use node-fetch? You call fetch(url) , which returns a promise
Use import fetch from 'node-fetch' . You must have "type": "module" in your package.json .