Image Processing: How to download an image using Node.js

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


How do you download a file?

I asked myself this question when I had to download a file from a server, programmatically.

I had to connect to a server, download a file, and store it locally.

This is the code I used:

const fs = require('fs')
const request = require('request')

const download = (url, path, callback) => {
  request.head(url, (err, res, body) => {
    request(url)
      .pipe(fs.createWriteStream(path))
      .on('close', callback)
  })
}

const url = 'https://…'
const path = './images/image.png'

download(url, path, () => {
  console.log('✅ Done!')
})

The code uses the fs built-in module and the request module.

request must be installed:

npm install request

Note that the request module was deprecated, which means it’s “complete” and no new changes will be applied to it. Only fixes. It doesn’t mean it will stop working and it does not mean we should stop using it.

Lessons in this unit:

0: Introduction
1: How to create and save an image with Node.js and Canvas
2: How to download an image from URL in Node
3: How to download and save an image using Node.js
4: How to get an image width and height using Node
5: How to turn an image into a data URI string
6: ▶︎ How to download an image using Node.js
7: How to print a canvas to a data URL
8: Optimize images from a Node.js script
9: Calling the ImageOptim macOS app from a Node.js script