Try it yourself with our free Base64 tool — runs entirely in your browser, no signup needed.

How to Base64 decode in Node.js

How to Base64 decode in Node.js

Base64 decoding is a crucial operation in many applications, allowing developers to convert encoded data back into its original form. In Node.js, Base64 decoding can be achieved using the built-in Buffer class. This guide will walk you through the process of Base64 decoding in Node.js, covering the most common use case, edge cases, common mistakes, and performance tips.

Quick Example

const buffer = require('buffer');

const base64String = 'SGVsbG8gd29ybGQh';
const decodedBuffer = Buffer.from(base64String, 'base64');
const decodedString = decodedBuffer.toString('utf8');

console.log(decodedString); // Output: "Hello world!"

This example decodes a Base64-encoded string SGVsbG8gd29ybGQh into its original form Hello world!.

Step-by-Step Breakdown

const buffer = require('buffer');

We start by importing the buffer module, which provides the Buffer class used for Base64 decoding.

const base64String = 'SGVsbG8gd29ybGQh';

We define the Base64-encoded string to be decoded.

const decodedBuffer = Buffer.from(base64String, 'base64');

We create a new Buffer instance from the Base64-encoded string, specifying the encoding as 'base64'. This will decode the Base64 string into a binary buffer.

const decodedString = decodedBuffer.toString('utf8');

We convert the decoded buffer to a string using the toString() method with the encoding set to 'utf8'. This will give us the original string that was encoded.

console.log(decodedString); // Output: "Hello world!"

Finally, we log the decoded string to the console.

Handling Edge Cases

Empty/Null Input

const base64String = null;
try {
  const decodedBuffer = Buffer.from(base64String, 'base64');
  console.log(decodedBuffer.toString('utf8'));
} catch (error) {
  console.error('Error decoding empty/null input:', error);
}

In this example, we attempt to decode a null input, which will throw a TypeError. We catch the error and log a message to the console.

Invalid Input

const base64String = 'Invalid base64 string';
try {
  const decodedBuffer = Buffer.from(base64String, 'base64');
  console.log(decodedBuffer.toString('utf8'));
} catch (error) {
  console.error('Error decoding invalid input:', error);
}

Here, we attempt to decode an invalid Base64 string, which will throw a TypeError. We catch the error and log a message to the console.

Large Input

const largeBase64String = '...large base64 string...';
const decodedBuffer = Buffer.from(largeBase64String, 'base64');
console.log(decodedBuffer.toString('utf8'));

When dealing with large Base64 strings, we can simply pass them to the Buffer.from() method, which will handle the decoding efficiently.

Unicode/Special Characters

const base64String = 'SGVsbG8gd29ybGQhIG1lbnQgc3BlY2lhbCBjaGFyYWN0ZXJzIQ==';
const decodedBuffer = Buffer.from(base64String, 'base64');
console.log(decodedBuffer.toString('utf8')); // Output: "Hello world! meet special characters!"

In this example, we decode a Base64 string containing Unicode characters, which are preserved in the decoded output.

Common Mistakes

Mistake 1: Not specifying the encoding

const decodedBuffer = Buffer.from(base64String); // Wrong!
const decodedBuffer = Buffer.from(base64String, 'base64'); // Correct!

Failing to specify the encoding will result in a TypeError.

Mistake 2: Not handling errors

const decodedBuffer = Buffer.from(base64String, 'base64');
console.log(decodedBuffer.toString('utf8')); // Wrong!
try {
  const decodedBuffer = Buffer.from(base64String, 'base64');
  console.log(decodedBuffer.toString('utf8'));
} catch (error) {
  console.error('Error decoding:', error);
} // Correct!

Not handling errors can lead to unexpected behavior or crashes.

Mistake 3: Using the wrong encoding

const decodedBuffer = Buffer.from(base64String, 'utf8'); // Wrong!
const decodedBuffer = Buffer.from(base64String, 'base64'); // Correct!

Using the wrong encoding will result in incorrect decoding.

Performance Tips

Tip 1: Use Buffer.from() instead of new Buffer()

const decodedBuffer = new Buffer(base64String, 'base64'); // Slow!
const decodedBuffer = Buffer.from(base64String, 'base64'); // Fast!

Using Buffer.from() is generally faster than creating a new Buffer instance.

Tip 2: Avoid unnecessary conversions

const decodedBuffer = Buffer.from(base64String, 'base64');
const decodedString = decodedBuffer.toString('utf8');
console.log(decodedString); // Slow!
console.log(decodedBuffer.toString('utf8')); // Fast!

Avoid unnecessary conversions between buffers and strings to improve performance.

Tip 3: Use streaming for large inputs

const fs = require('fs');
const readableStream = fs.createReadStream('large-base64-file.txt');
const writableStream = fs.createWriteStream('decoded-file.txt');
readableStream.pipe(writableStream);

For large inputs, consider using streaming to improve performance and memory usage.

FAQ

Q: What is the difference between Base64 and UTF-8?

A: Base64 is an encoding scheme that converts binary data to a string, while UTF-8 is a character encoding scheme that represents Unicode characters as a sequence of bytes.

Q: Can I use Base64 to encode arbitrary data?

A: Yes, Base64 can be used to encode arbitrary data, including binary data, text, and more.

Q: How do I handle Base64 decoding errors?

A: You can handle Base64 decoding errors by wrapping the decoding operation in a try-catch block and catching any errors that occur.

Q: Can I use Base64 to encode large files?

A: Yes, Base64 can be used to encode large files, but it's recommended to use streaming for large inputs to improve performance and memory usage.

Q: Is Base64 decoding secure?

A: Base64 decoding itself is not secure, as it does not provide any encryption or authentication. However, it can be used as part of a larger secure protocol.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp