Book
Now

Free | Node Fetch Download File

: This version is ESM-only . You must use import rather than require() . It is designed to be more compliant with the browser's WHATWG Fetch Standard.

import fetch from 'node-fetch'; import { createWriteStream } from 'fs'; import { pipeline } from 'stream/promises'; async function downloadFile(url, outputPath) { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch ${url}: ${response.statusText}`); } // response.body is a Node.js Readable stream in node-fetch await pipeline(response.body, createWriteStream(outputPath)); console.log('Download complete!'); } downloadFile('https://example.com', './local-image.png') .catch(console.error); Use code with caution. 2. Version Differences: v2 vs. v3 node fetch download file

The implementation details change slightly depending on your version of node-fetch . : This version is ESM-only

Downloading files with node-fetch is a common task in Node.js applications, but the "best" way to do it depends on whether you are using the older or the modern v3+ (ESM) . import fetch from 'node-fetch'; import { createWriteStream }

book now