Ben Borgers / Posts /

How to download an image with node-fetch

March 28, 2021 by @benborgers

node-fetch is a great package that allows you to use the wonderful fetch API in server-side Node.js. It can also be used to request and download images from a URL to the local filesystem!

Here's how you do it, assuming that you have node-fetch already installed ( by running npm install node-fetch).

const fs = require('fs') // Built-in filesystem package for Node.js
const fetch = require('node-fetch')

const imageUrl = ''

    .then(res =>

This downloads the image at imageUrl and "pipes" (downloads) it to the given path.

Note: Make sure that the folder you're downloading into exists already. If it doesn't, you'll want to create the folders required by doing something like:

const fs = require('fs')

fs.mkdirSync('./path/to', { recursive: true })

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!