Latest and greatest way to do this:

Node supports file and buffer operations with the base64 encoding:

const fs = require('fs');
const contents = fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});

Or using the new promises API:

const fs = require('fs').promises;
const contents = await fs.readFile('/path/to/file.jpg', {encoding: 'base64'});
Answer from Mike Martin on Stack Overflow
🌐
ICODEALOT
icodealot.com › convert-data-to-base64-in-nodejs
Convert data to Base64 in Node.js - ICODEALOT.COM
May 30, 2016 - The program will read the contents of a file identified by argv[2] and output that content as a Base64 encoded string. ... NB: My source file was XML-based. I did not test this program against binary data so you may need to alter the program to suite your own needs if you run into issues, although it should work.
🌐
GitHub
gist.github.com › crspiccin › 790796a68e7178404de4
Node.js convert an image to Base 64 · GitHub
// function to encode file data to base64 encoded string function base64_encode(file) { // read binary data const bitmap = fs.readFileSync(file); // convert binary data to base64 encoded string return bitmap.toString('base64'); } Copy link · ...
🌐
Dirask
dirask.com › posts › Node-js-convert-file-to-base64-DLr5Kj
Node.js - convert file to base64
const fs = require('fs'); // relative path to the file const filePath = './file.txt'; // reusable arrow function to encode file data to base64 encoded string const convertBase64 = (path) => { // read binary data from file const bitmap = fs.readFileSync(path); // convert the binary data to base64 encoded string return bitmap.toString('base64'); }; // Usage example const result = convertBase64(filePath); File system | Node.js v16.9.1 Documentation ·
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-nodejs
Base64 with NodeJS | Binary Encoding Techniques Across Programming Languages
Node.js makes it straightforward to work with file data using Base64 encoding. You can read file content directly into a Buffer, which is then easily converted to a Base64 string for transmission or storage.
🌐
npm
npmjs.com › package › js-base64-file
js-base64-file - npm
February 24, 2021 - This is an ideal way to load and convert local and remote files to base64 either for use as a string or to save on disk.
      » npm install js-base64-file
    
Published: Feb 24, 2021
Version: 2.0.3
🌐
YouTube
youtube.com › watch
Converting file into base64 string in nodejs - YouTube
App link:https://play.google.com/store/apps/details?id=niks.viks.ionic4 please subscribe: https://bit.ly/35Vj1en​WhatsApp:https://shorturl.at/gkGH0​Facebook:...
Published: June 21, 2019
Find elsewhere
🌐
npm
npmjs.com › package › file-base64
file-base64 - npm
Encode a file to a base64 string · Dncode a base64 string to a file · $ npm install file-base64 --save ·
      » npm install file-base64
    
Published: Nov 07, 2015
Version: 1.0.0
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-base64-encoding-and-decoding-is-done-in-node-js
How Base64 Encoding and Decoding is Done in Node.js ? - GeeksforGeeks
January 8, 2025 - NodeJS File System · NodeJS Globals ... built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class....
🌐
Stack Abuse
stackabuse.com › bytes › converting-images-and-image-urls-to-base64-in-node-js
Converting Images and Image URLs to Base64 in Node.js
August 19, 2023 - In Node.js, converting an image URL to Base64 can be achieved using the request module along with the built-in Buffer class. This Base64 data can then be embedded directly into an HTML image tag, allowing the image to be displayed within the web page without linking to an external file.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
August 22, 2017 - Take a look at the following example. Here we will encode a text string to Base64 using the Buffer object. Save the following code in a file encode-text.js.
🌐
Base64
base64.dev › home › articles › base64 in node.js
Base64 in Node.js — Encode & Decode with Buffer
July 3, 2026 - function toBase64URL(str) { return Buffer.from(str).toString('base64') .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } function fromBase64URL(b64url) { let b64 = b64url.replace(/-/g, '+').replace(/_/g, '/'); while (b64.length % 4) b64 += '='; return Buffer.from(b64, 'base64').toString('utf8'); } const fs = require('fs'); // Encode a file to Base64 string const fileBuffer = fs.readFileSync('image.png'); const encoded = fileBuffer.toString('base64'); console.log(`Encoded size: ${encoded.length} chars`); // Decode Base64 back to a file const decoded = Buffer.from(encoded, 'base64'); fs.writeFileSync('output.png', decoded);
🌐
npm
npmjs.com › package › nodejs-base64-converter
nodejs-base64-converter - npm
skip to:contentpackage searchsign in · npm · Sign UpSign In · 1.0.5 • Public • Published 6 years ago · Readme · Code Beta · 0 Dependencies · 7 Dependents · 5 Versions · var nodeBase64 = require('nodejs-base64-converter'); ...
      » npm install nodejs-base64-converter
    
Published: May 09, 2020
Version: 1.0.5
🌐
ItSolutionstuff
itsolutionstuff.com › post › node-js-convert-image-file-to-base64-string-exampleexample.html
Node JS Convert Image File to Base64 String Example - ItSolutionstuff.com
March 24, 2021 - In this example, you will learn node js convert image file to base64. you will learn node js convert base64 string to image. you can see node js image to base64 string convert.
🌐
Medium
medium.com › @damien_16960 › convert-file-to-base64-both-frontend-backend-typescript-d4cd31e98948
Convert file to base64 both Frontend & Backend Typescript | by Damien | Medium
July 7, 2024 - const toBase64 = (file: File): Promise<string> => new Promise((resolve, reject) => { const reader = new FileReader() reader.readAsDataURL(file) reader.onload = () => resolve(reader.result as string) reader.onerror = (error) => reject(error) }) const files = Array.from(fileList) as Array<File> const base64Files = await Promise.all( files.map(async (minifiedFile) => { const base64File = await toBase64(minifiedFile) return base64File }), )