- Use the
fs
module to require the built-in Node.js file system module:
const fs = require('fs');
- Use the
https
or http
module to make a GET request to download the file from a URL:
const https = require('https');
const fileUrl = 'https://example.com/file.zip';
- Create a writable stream using
fs.createWriteStream
to save the downloaded file locally:
const file = fs.createWriteStream('file.zip');
- Use
https.get
or http.get
to initiate the GET request and save the response to the file stream:
https.get(fileUrl, function(response) {
response.pipe(file);
});