How to Download a file with Node.js?

To download a file in Node.js, you can use the https and fs packages, or you can also use http. Here is an example of code you can use.

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

let fonts = ['https://ahmadrosid.com/profile.png'];

fonts.forEach(item => {
    const fileUrl = item;
    const path = './';

    https.get(fileUrl, response => {
        const fileName = fileUrl.split('/').pop();
        const filePath = `${path}/${fileName}`;

        response.pipe(fs.createWriteStream(filePath));
    });
})

Now you can save it to file download.js. And then run the script using this command.

node download.js