How to download an image with node-fetch
March 28, 2021
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 = 'https://via.placeholder.com/350x150'
fetch(imageUrl)
.then(res =>
res.body.pipe(fs.createWriteStream('./path/to/image.png'))
)
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 })
Subscribe to my newsletter!
A weekly round-up of new blog posts and updates to projects Iâm working on.