Table of Contents
Images are crucial part of the experience on the web.
As resources, images are pointed to using URLs, meaning if you have a URL, you can download the image.
In this post, we'll learn how to download an image from a URL using Node.
Download an image from a URL
To download an image, first we need a URL:
JAVASCRIPTconst url = "https://sabe.io/images/saturn.png";
Since we know we will eventually save this file to disk, let's import the fs module:
JAVASCRIPTimport { promises as fs } from "fs";
const url = "https://sabe.io/images/saturn.png";
Now, we can make use of the native fetch function in Node, so we don't need to import libraries like axios or requests.
Simply pass in the URL to make the request:
JAVASCRIPTimport { promises as fs } from "fs";
const url = "https://sabe.io/images/saturn.png";
const response = await fetch(url);
Now comes the tricky part. We need to take this response, convert it to a blob. With this blob, we then have to convert it to an ArrayBuffer, then finally create a normal Buffer from this ArrayBuffer.
The goal is ultimately, to be able to take this Buffer and write it to disk.
Here's how this looks:
JAVASCRIPTimport { promises as fs } from "fs";
const url = "https://sabe.io/images/saturn.png";
const response = await fetch(url);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
Finally, now that we have our Buffer, we can write it to a file with a name of your choice:
JAVASCRIPTimport { promises as fs } from "fs";
const url = "https://sabe.io/images/saturn.png";
const response = await fetch(url);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.writeFile("./saturn.png", buffer);
This works, but we can convert this into a reusable function for repeated usage:
JAVASCRIPTimport { promises as fs } from "fs";
const downloadImage = async (url, path) => {
const response = await fetch(url);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.writeFile(path, buffer);
}
await downloadImage("https://sabe.io/images/saturn.png", "./saturn.png");
And that is how you use the native Node fetch function to download an image asynchronously using the await/async syntax.
Conclusion
In this post, we learned how to asynchronously download an image using Node without any additional libraries.
Simply use the native fetch function and convert the formats a few times to get a buffer that you can write to disk.
Thanks for reading!
How to Install Node on Windows, macOS and Linux
Getting Started with Electron
How to Serve Static Files with Nginx and Docker
How to Set Up Cron Jobs in Linux
How to build a Discord bot using TypeScript
How to deploy a MySQL Server using Docker
How to deploy an Express app using Docker
Using Puppeteer and Jest for End-to-End Testing
Getting Started with Handlebars.js
Build a Real-Time Chat App with Node, Express, and Socket.io
Setting Up Stylus CSS Preprocessor
Using Axios to Pull Data from a REST API
