: Includes a built-in fetch API , which is the preferred choice as it requires no external dependencies.
The most reliable way to handle a ZIP file is to pipe the response body directly into a file system write stream. This prevents the entire binary file from being loaded into RAM at once. 1. Using Modern Node.js (Built-in Fetch) node-fetch download zip file
When you need to automate file transfers in a backend environment, using node-fetch to download a ZIP file is a common and efficient solution. While simple GET requests work for small files, ZIP files are often large and require to prevent your application from consuming too much memory. Choosing Your Version : Includes a built-in fetch API , which
Before starting, identify which version of node-fetch or Node.js you are using: Choosing Your Version Before starting
import fs from 'fs'; import { Readable } from 'stream'; import { finished } from 'stream/promises'; async function downloadZip(url, destination) { const response = await fetch(url); if (!response.ok) throw new Error(`Failed to fetch: ${response.statusText}`); const fileStream = fs.createWriteStream(destination); // Convert the web stream to a Node-compatible stream and pipe it await finished(Readable.fromWeb(response.body).pipe(fileStream)); console.log("Download complete!"); } Use code with caution. 2. Using node-fetch v2 (CommonJS)
: An ESM-only module. You must use import instead of require .