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.
Decoding is the reverse operation.
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.
To decode and write the file back:
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.
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.
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
base64urlis 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
base64urlwhen the output is meant for URLs or compact tokens. - Base64 is a transport format, not a security mechanism.

