How to download a file using Node.js?

29 Jan, 2021
Downloading a file using node js can be done using inbuilt packages or with third party libraries.
Method 1: Using ‘https’ and ‘fs’ module
GET method is used on HTTPS to fetch the file which is to be downloaded. createWriteStream() is a method that is used to create a writable stream and receives only one argument, the location where the file is to be saved. pipe() is a method that reads the data from the readable stream and writes it onto the writable stream.
Javascript
const fs = require('fs');const https = require('https');  // URL of the imageconst url = 'GFG.jpeg';  https.get(url,(res) => {    // Image will be stored at this path    const path = `${__dirname}/files/img.jpeg`;     const filePath = fs.createWriteStream(path);    res.pipe(filePath);    filePath.on('finish',() => {        filePath.close();        console.log('Download Completed');     })})
Method 2: Using third party libraries‘node-downloader-helper’ LibraryInstallation: npm install node-helper-libraryBelow is the code for downloading an image from a website. An object dl is created of class DownloadHelper which receives two arguments:The image which is to be downloaded.The path where the image has to be saved after downloading.The file variable contains the URL of the image which will be downloaded and filePath variables contain the path where the file will be saved.JavascriptJavascriptconst { DownloaderHelper } = require('node-downloader-helper');  // URL of the imageconst file = 'GFG.jpeg';// Path at which image will be downloadedconst filePath = `${__dirname}/files`;   const dl = new DownloaderHelper(file , filePath);  dl.on('end', () => console.log('Download Completed'))dl.start();‘download’ LibraryInstallation:npm install downloadBelow is the code for downloading an image from a website. The download function receives the file and path of fileJavascriptJavascriptconst download = require('download');  // Url of the imageconst file = 'GFG.jpeg';// Path at which image will get downloadedconst filePath = `${__dirname}/files`;  download(file,filePath).then(() => {    console.log('Download Completed');})console output of all the above three codes
Method 2: Using third party libraries
‘node-downloader-helper’ LibraryInstallation: npm install node-helper-libraryBelow is the code for downloading an image from a website. An object dl is created of class DownloadHelper which receives two arguments:The image which is to be downloaded.The path where the image has to be saved after downloading.The file variable contains the URL of the image which will be downloaded and filePath variables contain the path where the file will be saved.JavascriptJavascriptconst { DownloaderHelper } = require('node-downloader-helper');  // URL of the imageconst file = 'GFG.jpeg';// Path at which image will be downloadedconst filePath = `${__dirname}/files`;   const dl = new DownloaderHelper(file , filePath);  dl.on('end', () => console.log('Download Completed'))dl.start();
Installation: 
npm install node-helper-library
Below is the code for downloading an image from a website. An object dl is created of class DownloadHelper which receives two arguments:
The image which is to be downloaded.
The path where the image has to be saved after downloading.
The file variable contains the URL of the image which will be downloaded and filePath variables contain the path where the file will be saved.
Javascript
const { DownloaderHelper } = require('node-downloader-helper');  // URL of the imageconst file = 'GFG.jpeg';// Path at which image will be downloadedconst filePath = `${__dirname}/files`;   const dl = new DownloaderHelper(file , filePath);  dl.on('end', () => console.log('Download Completed'))dl.start();
‘download’ LibraryInstallation:npm install downloadBelow is the code for downloading an image from a website. The download function receives the file and path of fileJavascriptJavascriptconst download = require('download');  // Url of the imageconst file = 'GFG.jpeg';// Path at which image will get downloadedconst filePath = `${__dirname}/files`;  download(file,filePath).then(() => {    console.log('Download Completed');})console output of all the above three codes
Installation:
npm install download
Below is the code for downloading an image from a website. The download function receives the file and path of file
Javascript
const download = require('download');  // Url of the imageconst file = 'GFG.jpeg';// Path at which image will get downloadedconst filePath = `${__dirname}/files`;  download(file,filePath).then(() => {    console.log('Download Completed');})
console output of all the above three codes
NodeJS-Questions
Picked
Technical Scripter 2020
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React