Categories
JavaScript Answers

How to read file into a base64 string in Node.js?

Spread the love

To read file into a base64 string in Node.js, we call the readFile method.

For instance, we write

const fs = require("fs").promises;
const contents = await fs.readFile("/path/to/file.jpg", { encoding: "base64" });

to call the readFile method to read the /path/to/file.jpg file into a base64 string by setting the encoding option to 'base64'.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *