Skip to content

Node.js: Download an Image from the URL using Axios

In this post, you will learn how to download an image from the URL using Axios, node.js

Step 1: Install Axios

Make sure you have Axios installed in your Node.js project. If you haven’t installed it yet, you can do so by running the following command from your project directory.

npm install axios

Step 2: Write the Download Code

Create a JavaScript file (e.g., downloadImage.js) and write the following code:

const axios = require('axios');
const fs = require('fs');

const imageUrl = ''; // Replace with your image URL
const downloadPath = 'path/to/save/image.jpg'; // Replace with your desired download path

const downloadImage = async () => {
  try {
    const response = await axios({
      method: 'GET',
      url: imageUrl,
      responseType: 'stream',

    // Create a write stream to save the image
    const writer = fs.createWriteStream(downloadPath);

    // Pipe the response data into the writer stream;

    // Wait for the image to finish downloading
    await new Promise((resolve, reject) => {
      writer.on('finish', resolve);
      writer.on('error', reject);

    console.log('Image downloaded successfully!');
  } catch (error) {
    console.error('Error downloading image:', error.message);

// Call the downloadImage function

In the code above, replace with the actual URL of the image, you want to download. Also, specify the desired path and filename where you want to save the downloaded image by replacing 'path/to/save/image.jpg' with your desired path.

Step 3: Run the Script Save the file and run it using Node.js:

node downloadImage.js

The script will download the image from the specified URL and save it to the specified path. Make sure the directory where you want to save the image exists and has write permissions.

That’s it! After running the script, you should see a success message if the image was downloaded successfully, or an error message if any issues occurred during the download process.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments