As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string:

var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da

For Node.js v5.11.1 and below

Construct a new Buffer and pass 'base64' as the second argument:

var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da

If you want to be clean, you can check whether from exists :

if (typeof Buffer.from === "function") {
    // Node 5.10+
    buf = Buffer.from(b64string, 'base64'); // Ta-da
} else {
    // older Node versions, now deprecated
    buf = new Buffer(b64string, 'base64'); // Ta-da
}
Answer from Matt Ball on Stack Overflow
๐ŸŒ
Node.js
nodejs.org โ€บ api โ€บ buffer.html
Buffer | Node.js v26.11.1 Documentation
import { Buffer } from 'node:buffer'; const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); console.log(buf); // Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64> const { Buffer } = require('node:buffer'); const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); console.log(buf); // Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
๐ŸŒ
Base64Encode.org
base64encode.org โ€บ enc โ€บ buffer
Base64 Encoding of "buffer" - Online
Other variations, usually derived from Base64, share this property but differ in the symbols chosen for the last two values; an example is the URL and filename safe "RFC 4648 / Base64URL" variant, which uses "-" and "_". ... In the above quote the encoded value of Man is TWFu. Encoded in ASCII, the letters "M", "a", and "n" are stored as the bytes 77, 97, 110, which are equivalent to "01001101", "01100001", and "01101110" in base-2. These three bytes are joined together in a 24 bit buffer producing the binary sequence "010011010110000101101110".
Discussions

How can I do Base64 encoding in Node.js? - Stack Overflow
This error happens on node versions older than v6. In these versions you'll need to use the constructor (new Buffer) rather than Buffer.from. 2016-09-28T02:13:23.467Z+00:00 ... Buffer.from() was added in v4.5.0. More on stackoverflow.com
๐ŸŒ stackoverflow.com
buffer.write(buffer, 'base64')
What's the fastest way to convert a buffer containing base64-encoded data to a buffer containing binary data? More on github.com
๐ŸŒ github.com
16
March 15, 2017
How can I convert a base64 string to ArrayBuffer?
You get access to the array buffer through the .buffer property of what is returned from Uint8Array 2018-02-07T19:22:04.97Z+00:00 ... @Saites, There's nothing wrong with atob or btoa, you just have to give them valid input. atob needs a valid base64 string, otherwise it will throw an error. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert buffer base64 -> utf8 encoding node.js
new Buffer(...) has been deprecated for a while now, go for Buffer.from(...) ... This only works if base64encoded is a string. If it's already a buffer you get the same encoded value out as what went in. Only thing I've found is to dump the already-encoded buffer to a base64, read it back into ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-base64-encoding-and-decoding-is-done-in-node-js
How Base64 Encoding and Decoding is Done in Node.js ? | GeeksforGeeks
January 8, 2025 - The Buffer.from() method is again used to convert the base64 string back to bytes, however, this time specifying the current encoding as "base64". The converted bytes can then be returned as the original utf8 string ...
๐ŸŒ
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 ยท
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.

Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ convert-base64-string-to-arraybuffer-in-javascript
Convert base64 String to ArrayBuffer In JavaScript - GeeksforGeeks
August 5, 2025 - Often used to encode binary data in text-based formats like JSON or HTML, it needs to be converted back into its original binary format for further processing. An ArrayBuffer in JavaScript is a generic, fixed-length binary data buffer, which is ...
๐ŸŒ
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.
๐ŸŒ
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'
Author: nodejs
๐ŸŒ
Bun
bun.com โ€บ guides โ€บ utilities โ€บ base64 encoding
Encode and decode base64 data | Bun Guides
const encoded = Buffer.from("hello world").toBase64(); // => "aGVsbG8gd29ybGQ=" const bytes = Buffer.from(encoded, "base64"); bytes instanceof Uint8Array; // => true const text = bytes.toString("utf8"); // => "hello world"
๐ŸŒ
Medium
hyunbinseo.medium.com โ€บ base64-in-node-js-and-browser-c7fba48ae033
Base64 in Node.js and browser. Choosing between Buffer and atob (btoa) | by Hyunbin | Medium
March 5, 2022 - For code running using Node.js ...base64โ€™). Source: node_modules/@types/node/buffer.d.ts ยท Base64 can be converted to and from Base64URL without any libraries....
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ nodejs-base64-encode-decode
Encoding and decoding Base64 strings in Node.js
October 1, 2022 - To convert a string into a Base64 encoded string, we first create a buffer from the given string using the Buffer.from() method. This method takes two parameters, a plain-text string, and the character encoding, and creates a buffer or binary ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ node-js-base64-encoding-and-decoding
Node.js - Buffer.toString() Method
August 16, 2021 - const buffer = Buffer.from('Hello'); console.log(buffer.toString('hex'));
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ tools โ€บ base64 encoder / decoder
Base64 Encoder and Decoder Online | Coddy
April 25, 2026 - Paste the string into a Base64 decoder. The output is the original text or file. In code, most languages ship a built-in: atob() in browsers, Buffer.from(str, 'base64') in Node.js, base64.b64decode() in Python. Is Base64 secure? No. Base64 is encoding, not encryption.
๐ŸŒ
DEV Community
dev.to โ€บ dnature โ€บ convert-a-base64-data-into-an-image-in-node-js-3f88
Convert a Base64 data into an Image in Node.js - DEV Community
December 17, 2021 - Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding. Buffer is available as a global object which means that you don't need to explicitly require this module in your application.