Node.js
Base64 Encoding
Programming
JavaScript
Data Encoding

How can I do Base64 encoding in Node.js?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Node.js, Base64 encoding is usually a one-line operation with Buffer. The important part is understanding what you are encoding, because text, binary files, and URL-safe tokens all have slightly different handling requirements.

Core Sections

Encode text with Buffer

For ordinary strings, create a buffer from the text and convert it to a Base64 string.

javascript
const text = "Hello, world!";
const base64 = Buffer.from(text, "utf8").toString("base64");
console.log(base64);

Decoding is the reverse operation.

javascript
const original = Buffer.from(base64, "base64").toString("utf8");
console.log(original);

This is the standard solution for most “how do I do Base64 in Node.js” questions.

Encode binary data, not just strings

Base64 is especially useful for binary content such as images, PDFs, or arbitrary file bytes. In those cases, avoid converting the file to text first. Read the bytes into a buffer and encode that buffer directly.

javascript
1const fs = require("node:fs");
2
3const imageBytes = fs.readFileSync("logo.png");
4const imageBase64 = imageBytes.toString("base64");
5console.log(imageBase64.slice(0, 40));

To decode and write the file back:

javascript
const decodedBytes = Buffer.from(imageBase64, "base64");
fs.writeFileSync("logo-copy.png", decodedBytes);

This preserves the original binary data correctly.

Build data URLs when needed

If you want to embed binary content directly in HTML or CSS, Base64 is often combined with a MIME type inside a data URL.

javascript
const dataUrl = `data:image/png;base64,${imageBase64}`;
console.log(dataUrl.slice(0, 60));

The Base64 string alone is not enough for that use case. The consumer also needs the media type prefix. This pattern is common for small embedded previews, generated emails, and APIs that need to transport binary content through text-only payloads.

Know when Base64 increases size

Base64 is encoding, not compression. It expands the data size by roughly one third. That tradeoff is acceptable when you need a text-safe representation, but it is not free.

For large payloads, sending raw binary is often better when the transport supports it. Use Base64 when the interface demands text or when a textual container such as JSON is unavoidable.

Use Base64 URL format when the value goes into URLs or tokens

Standard Base64 uses +, /, and padding = characters. Those are inconvenient in URLs and tokens. Newer Node.js versions support base64url directly.

javascript
1const token = Buffer.from("user:42", "utf8").toString("base64url");
2console.log(token);
3
4const decoded = Buffer.from(token, "base64url").toString("utf8");
5console.log(decoded);

This is better than manually replacing characters because the intent is explicit and the decoding path stays symmetrical.

Do not confuse encoding with security

Base64 is reversible. Anyone who can read the string can decode it. It is fine for transport formatting, but it does not protect secrets.

If you need confidentiality, use encryption. If you need integrity, use signatures or message authentication. Base64 only changes representation.

Common Pitfalls

  • Encoding binary data by first converting it to a normal text string, which can corrupt bytes before the Base64 step even happens.
  • Forgetting which character encoding was used for the original text, which leads to incorrect decoding later.
  • Using standard Base64 in URLs or tokens when base64url is the better fit.
  • Treating Base64 as if it were encryption, even though it is trivial to reverse.
  • Embedding large files as Base64 everywhere without considering the size overhead and memory cost.

Summary

  • In Node.js, Base64 encoding is normally Buffer.from(...).toString("base64").
  • Decode with Buffer.from(value, "base64") and convert back to text or bytes as needed.
  • Encode binary content directly from buffers, not through intermediate text conversions.
  • Use base64url when the output is meant for URLs or compact tokens.
  • Base64 is a transport format, not a security mechanism.

Course illustration
Course illustration

All Rights Reserved.