Open In App

How to Download a File Using Node.js?

Last Updated : 08 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Downloading files from the internet is a common task in many Node.js applications, whether it’s fetching images, videos, documents, or any other type of file. In this article, we’ll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.

Using Built-in Modules

Using the http or https Module

Node.js has built-in modules like ‘https’ and ‘http’ for making HTTP requests. You can utilize these modules to download files from a remote server. The GET method is used on HTTPS to retrieve the file to be downloaded. The ‘createWriteStream()’ method is used to create a writable stream and takes only one argument, which is the location where the file is to be saved. Additionally, the ‘pipe()’ method reads the data from the readable stream and writes it to the writable stream.

Example: Implementation to show how to download files using an HTTP module.

const http = require('http');
const fs = require('fs');

const fileUrl = 'http://example.com/file.txt';
const destination = 'downloaded_file.txt';

const file = fs.createWriteStream(destination);

http.get(fileUrl, (response) => {
    response.pipe(file);
    file.on('finish', () => {
        file.close(() => {
            console.log('File downloaded successfully');
        });
    });
}).on('error', (err) => {
    fs.unlink(destination, () => {
        console.error('Error downloading file:', err);
    });
});

Step to Run Application: Run the application using the following command from the root directory of the project

node .\app.js
Screenshot-2024-06-07-165929

Using third party libraries

The node-downloader-helper library provides a convenient and feature-rich solution for downloading files in Node.js applications. It simplifies the process of downloading files from the internet by handling tasks such as progress tracking, resuming interrupted downloads, and error handling. In this article, we’ll explore how to use the node-downloader-helper library to download files in Node.js.

Installation: 

npm install node-helper-library

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.

Example: Implementation to show how to download files using third party libraries.

</p><pre><code class="language-node"></code></pre><p></p><pre></pre><p><br></p><pre><code><span>const { DownloaderHelper } = require('node-downloader-helper');</span></code><br><br><code><span>// URL of the image</span></code><br><code><span>const file = 'GFG.jpeg';</span></code><br><code><span>// Path at which image will be downloaded</span></code><br><code><span>const filePath = `${__dirname}/files`; </span></code><br><br><code><span>const dl = new DownloaderHelper(file , filePath);</span></code><br><br><code><span>dl.on('end', () => console.log('Download Completed'))</span></code><br><code><span>dl.start();</span></code><br></pre><p dir="ltr"><br></p><p dir="ltr"><b><strong>Step to Run Application:</strong></b><span> Run the application using the following command from the root directory of the project</span></p><pre><span>node .\app.js</span></pre>[caption width="800"]<img src="https://media.geeksforgeeks.org/wp-content/uploads/20240607170621/Screenshot-2024-06-07-165929.png" alt="Screenshot-2024-06-07-165929" width="601" height="157" srcset="https://media.geeksforgeeks.org/wp-content/uploads/20240607170621/Screenshot-2024-06-07-165929.png 601w,https://media.geeksforgeeks.org/wp-content/uploads/20240607170621/Screenshot-2024-06-07-165929-100.png 100w,https://media.geeksforgeeks.org/wp-content/uploads/20240607170621/Screenshot-2024-06-07-165929-200.png 200w,https://media.geeksforgeeks.org/wp-content/uploads/20240607170621/Screenshot-2024-06-07-165929-300.png 300w" loading="lazy"> [/caption]<p dir="ltr"><br></p><p dir="ltr"><span>Below is the code for downloading an image from a website. An object </span><b><strong>dl </strong></b><span>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.</span></p><h2 id="using-download-library"><b><strong>Using download Library</strong></b></h2><p dir="ltr"><span>The </span><code><span>download</span></code><span> library simplifies the process of downloading files in Node.js applications by providing a straightforward API for making HTTP requests and saving the downloaded files to the local file system. In this article, we'll explore how to use the </span><code><span>download</span></code><span> library to download files in Node.js.</span></p><h3 id="installation-1"><span>Installation:</span></h3><pre><span>npm install download</span></pre><p dir="ltr"><b><strong>Example:</strong></b><span> Implementation to show how to download files using </span><b><strong>download</strong></b><span> libraries.</span></p><p dir="ltr"><gfg-tabs data-run-ide="false" data-mode="light"><gfg-tab slot="tab">Node



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');
})


Step to Run Application: Run the application using the following command from the root directory of the project

node .\app.js

console output of all the above three codes




Similar Reads

three90RightbarBannerImg