How to download a file using Node.js? Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report 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 image const 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’ Library 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 image const file = 'GFG.jpeg'; // Path at which image will be downloaded const filePath = `${__dirname}/files`; const dl = new DownloaderHelper(file , filePath); dl.on('end', () => console.log('Download Completed')) dl.start(); ‘download’ Library 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 image const file = 'GFG.jpeg'; // Path at which image will get downloaded const filePath = `${__dirname}/files`; download(file,filePath) .then(() => { console.log('Download Completed'); }) console output of all the above three codes Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now! Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 29 Jan, 2021 Like Article Save Article Previous How to use an ES6 import in Node? Next Explosion Animation in Canvas Share your thoughts in the comments Add Your Comment Please Login to comment...