Convert a Base64 data into an Image in Node.js

Image Base64 Encoding and Decoding in Node.js

Learn how to convert an image into a base64 string and back to an image.


You will also learn how to compress images with Jimp.

Recently, I got involved in a project where images are returned from the browser in base64 format and we need to write the image to disk. I did some research to see if i could get any detailed explanation on how things work with base64, Buffer, binary and real images but couldn't find any solution related to nodejs.

Trying to get this to work directly by reading an image with the fs.readFile... method and piping in it back to an image would work perfectly because by default, fs.readFile... returns a Buffer and that is what we need. Here's an example.



const fs = require("fs");
// Reads file in form buffer => <Buffer ff d8 ff db 00 43 00 ...
const buffer = fs.readFileSync("path-to-image.jpg");
// Pipes an image with "new-path.jpg" as the name.
fs.writeFileSync("new-path.jpg", buffer);

Things tend to get a bit more complicated when you want to convert a regular (base64) string into an actual image. You need to first convert your string into Buffer before saving it as a real image otherwise, you're going to run into issues.

// Base64 => Buffer => Image

Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding. Buffer is available as a global object which means that you don't need to explicitly require this module in your application.

Internally, Buffer represents binary data in the form of a sequence of bytes. The Buffer object provides several methods to perform different encoding and decoding conversions. This includes to and from UTF-8, UCS2, Base64, ASCII, UTF-16, and even the HEX encoding scheme.

Base64 Encoding

Let us first of all convert our image into base64 and then to Buffer



const fs = require("fs");
// Create a base64 string from an image => ztso+Mfuej2mPmLQxgD ...
const base64 = fs.readFileSync("path-to-image.jpg", "base64");
// Convert base64 to buffer => <Buffer ff d8 ff db 00 43 00 ...
const buffer = Buffer.from(base64, "base64");

Base64 To Image

Now you can convert the Buffer into an actual image with just a single line of code:



// Pipes an image with "new-path.jpg" as the name.
fs.writeFileSync("new-path.jpg", buffer);


Image optimization is very important these days because most images come in huge sizes and this will definitely slow down our websites. I'll be using Jimp to show you how to reduce image quality on the fly by reducing the size of the Buffer data we receive and convert it into an actual image.

Install Jimp:



yarn add jimp
# or
npm install jimp

You can simply reduce the quality of your image by 50% with just a few lines of code. I am going to use a base64 string as my data source.



const Jimp = require("jimp");
const fs = require("fs");
// Base64 string
const data =
// Convert base64 to buffer => <Buffer ff d8 ff db 00 43 00 ...
const buffer = Buffer.from(data, "base64");, (err, res) => {
if (err) throw new Error(err);

I would love to give some extra credits to the creators of Jimp because it solves nearly everything you would want to do with an actual image in nodejs and has zero dependency. I've had zero issues using it in an electron application.


We were able to:

  1. Understand how images work in nodejs on different stages.
  2. Convert from Buffer to base64.
  3. Convert from Buffer into an actual image.
  4. Use Jimp to reduce image size.

If you made it to this point, then I'm going to say congratulations 🍻 because you're awesome :).

I hope you find this helpful.

Happy codding 💻 🙂