How to download a file using Node.js


To download a file using Node.js, use one of the the many npm request libraries.

Example

For example, an MP4 file can be downloaded using node-fetch.

Install node-fetch:

$ npm install node-fetch

The URL of the MP4 file is:

https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4

Create a JavaScript file index.js that downloads the MP4:

// index.js
const fetch = require('node-fetch');
const { writeFile } = require('fs').promises;

const url =
  'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4';

(async () => {
  const response = await fetch(url);
  const buffer = await response.buffer();
  await writeFile('test.mp4', buffer);
  console.log('Done!');
})();

Run the script to download the MP4:

$ node index.js

Demo

Repl.it:



Please support this site and join our Discord!