Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.

Answer from onteria_ on Stack Overflow
Top answer
1 of 8
2832

Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.

2 of 8
402

The accepted answer previously contained new Buffer(), which is considered a security issue in Node.js versions greater than 6 (although it seems likely for this use case that the input can always be coerced to a string).

The Buffer constructor is deprecated according to the documentation.

The code snippets should read:

console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));

After this answer was written, it has been updated and now matches this.

🌐
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 - To perform Base64 encoding and decoding in Node.js, use the Buffer class. Encode data with Buffer.from(data).toString('base64'), and decode with Buffer.from(base64String, 'base64').toString(). This approach works for both strings and binary data.
Discussions

buffer.write(buffer, 'base64')
Is there a way to write the base64 buffer directly into another buffer, without creating any interim strings? More on github.com
🌐 github.com
16
March 15, 2017
Convert String to base64
Buffer.from creates a buffer (aka raw binary data), not an encoded string, the argument that you provide is the encoding the string is currently in. In order to change the encoding, you have to encode it again: Buffer.from("some string", "utf8").toString("base64") Similar to reverse the process: Buffer.from("c29tZSBzdHJpbmc=", "base64").toString("utf8") More on reddit.com
🌐 r/learnjavascript
5
5
March 6, 2023
node.js - NodeJS: How to decode base64 encoded string back to binary? - Stack Overflow
I was implementing password hashing with salt, so I generated salt as binary, hashed the password, base64 encoded the password and salt then stored them into database. Now when I am checking passw... More on stackoverflow.com
🌐 stackoverflow.com
using Buffer.from(binaryData).toString('base64') produces invalid base64 data
Maybe because : and ; isn't a Base64 character? You only really need to store the data part of s img base64, so why not truncate the previous part, and only store the data part? More on reddit.com
🌐 r/node
1
2
July 25, 2022
🌐
Better Stack
betterstack.com › community › questions › how-to-do-base64-encoding-in-node-js
How can I do Base64 encoding in Node.js? | Better Stack Community
March 11, 2024 - The Buffer.from(base64String, 'base64') method creates a Buffer object from the Base64-encoded string. The toString('utf-8') method converts the Buffer object to a UTF-8 encoded string, representing the original string.
🌐
GitHub
github.com › nodejs › node › issues › 11866
buffer.write(buffer, 'base64') · Issue #11866 · nodejs/node
March 15, 2017 - var base64Buffer = ...; // e.g. A buffer containing base64 provided by a mime decoder. var binaryBuffer = new Buffer(base64Buffer.toString('ascii'), 'base64')
Author: nodejs
🌐
Attacomsian
attacomsian.com › blog › nodejs-base64-encode-decode
Encoding and decoding Base64 strings in Node.js
October 1, 2022 - The second parameter in Buffer.from() is optional when dealing with plain-text (UTF-8) strings. The Base64 decoding process is very much similar to the encoding process.
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.11.1 Documentation
For binary-to-text encodings, the naming convention is reversed: Converting a Buffer into a string is typically referred to as encoding, and converting a string into a Buffer as decoding. 'base64': Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept ...
Find elsewhere
🌐
Futurestud.io
futurestud.io › tutorials › how-to-base64-encode-decode-a-value-in-node-js
How to Base64 Encode/Decode a Value in Node.js
November 18, 2021 - // using the base64 encoded string from above const plain = Buffer.from('dXNlcm5hbWU6cGFzc3dvcmQ=', 'base64').toString('utf8') // 'username:password'
🌐
Nesin
nesin.io › blog › base64-encode-decode-nodejs
Base64 Encoding and Decoding with Node.js - Nesin.io
March 10, 2023 - // Base64 encoded string const base64 = 'VGhpcyB3aWxsIGJlIGVuY29kZWQgaW4gYmFzZTY0='; // create a buffer const buff = Buffer.from(base64, 'base64'); // decode buffer as UTF-8 const str = buff.toString('utf-8'); // print decoded string console.log(base64); // This will be encoded in base64 ·
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
November 25, 2024 - In Node.js, the Buffer object can be used to encode and decode Base64 strings to and from many other formats, allowing you to easily convert data back and forth as needed.
🌐
O'Reilly
oreilly.com › library › view › the-javascript-json › 9781785286902 › ch08s02.html
Encoding binary data as a base64 string using Node.js - The JavaScript JSON Cookbook [Book]
June 25, 2015 - Node.js provides the Buffer object and a base64 encoder and decoder for this task. First, you'll allocate a buffer, and then you'll convert it to a string, indicating that the string you want should be base64-encoded, ...
Author: Ray Rischpater
Published: 2015
Pages: 192
🌐
Reddit
reddit.com › r/node › using buffer.from(binarydata).tostring('base64') produces invalid base64 data
r/node on Reddit: using Buffer.from(binaryData).toString('base64') produces invalid base64 data
July 25, 2022 -

For context, I'm trying to store images in blob format in an sqlite database. I send the image in post data in base64 format, then when the request is received in express, I run

Buffer.from(req.body.img, 'base64'); to convert it into binary and store it in the database. However, when I retrieve the data and convert it back to base64, it produces an invalid base64 string like

dataimage/jpgbase64<base64data> when it should be data:image/jpg;base64,<base64data>

I'm using prisma to manage my database.

Is this an issue with the actual encoding/decoding, prisma, or sqlite?

Edit: encoding the base64 into binary, and then immediately encoding the binary back into base64 (without saving to the database) produces the same issue.

🌐
Bun
bun.sh › utilities › encode and decode base64 data
Encode and decode base64 data - Bun
July 7, 2026 - const encoded = Buffer.from("hello ... bytes = Buffer.from(encoded, "base64"); bytes instanceof Uint8Array; // => true const text = bytes.toString("utf8"); // => "hello world" The older btoa() and atob() APIs are still available for compatibility, ...
🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode from Base64 format or encode into it with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
GitHub
github.com › nodejs › node › commit › acba5dc87e
module: use buffer.toString base64 · nodejs/node@acba5dc
January 31, 2025 - const inlinedSourceMap = Buffer.from(JSON.stringify(sourceMap)).toString('base64'); 85 · + assert.strictEqual(script.sourceMapURL, `data:application/json;base64,${inlinedSourceMap}`); 86 · + }); 87 · + 88 · + test('stripTypeScriptTypes source map when mode is transform and sourceUrl with non-latin-1 chars', () => { 89 ·
Author: nodejs
🌐
Bun
bun.com › guides › utilities › base64 encoding
Encode and decode base64 data | Bun Guides
const bytes = new TextEncoder().encode("hello world"); const encoded = bytes.toBase64(); // => "aGVsbG8gd29ybGQ=" const decoded = Uint8Array.fromBase64(encoded); const text = new TextDecoder().decode(decoded); // => "hello world" Node.js Buffer extends Uint8Array, so you can encode buffers with toBase64() and pass them to APIs that accept Uint8Array. Buffer also provides Node.js-compatible base64 decoding with Buffer.from(encoded, "base64").
🌐
GitHub
github.com › oven-sh › bun › issues › 1184
Buffer.from is blank for specific base64 strings · Issue #1184 · oven-sh/bun
August 31, 2022 - Attempting to encode and then decode a JWT header produces an empty string instead of the original text: const encoded = Buffer.from('{"alg":"RS256","typ":"JWT"}', 'utf-8').toString('base64') // 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9' const decoded = Buffer.from(encoded, 'base64').toString('utf-8') // '' (empty string) Currently that's the only string I've found that causes the bug (but it's breaking the jws / jsonwebtoken Node packages) For most strings, this works: const encoded = Buffer.from(original, 'utf-8').toString('base64') const decoded = Buffer.from(encoded, 'base64').toString('utf-8') console.log(decoded === original) // true ·
Author: oven-sh
🌐
Coddy.Tech
coddy.tech › tools › base64 encoder / decoder
Base64 Encoder and Decoder Online | Coddy
April 25, 2026 - Each 6-bit group is mapped to one of 64 characters. If the input isn't a multiple of 3 bytes long, one or two = characters are added as padding. ... Paste the string into a Base64 decoder. The output is the original text or file.
🌐
npm
npmjs.com › package › base64-arraybuffer
base64-arraybuffer - npm
January 22, 2022 - The library encodes and decodes base64 to and from ArrayBuffers · encode(buffer) - Encodes ArrayBuffer into base64 string · decode(str) - Decodes base64 string to ArrayBuffer · You can run the test suite with: npm test · Copyright (c) 2012 ...
      » npm install base64-arraybuffer
    
Published: Jan 22, 2022
Version: 1.0.2