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
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
This is not the same as String.prototype.length, which does not account for the encoding that is used to convert the string into bytes. For 'base64', 'base64url', and 'hex', this function assumes valid input. For strings that contain non-base64/hex-encoded data (e.g. whitespace), the return value might be greater than the length of a Buffer created from the string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-base64-string-to-arraybuffer-in-javascript
Convert base64 String to ArrayBuffer In JavaScript - GeeksforGeeks
August 5, 2025 - Below are the approaches to convert base64 string to ArrayBuffer in Javascript: ... The atob() function decodes a Base64-encoded string into a binary string.
Discussions

javascript - Convert base64 string to ArrayBuffer - Stack Overflow
I need to convert a base64 encode string into an ArrayBuffer. The base64 strings are user input, they will be copy and pasted from an email, so they're not there when the page is loaded. I would li... More on stackoverflow.com
🌐 stackoverflow.com
April 27, 2017
javascript - How can I convert a base64 string to ArrayBuffer? - Stack Overflow
I need to convert a base64 encoding string into an ArrayBuffer. The base64 strings are user input, they will be copy and pasted from an email, so they're not there when the page is loaded. I would ... 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
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
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.

🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
November 25, 2024 - The same applies to images your ... This is where Base64 encoding comes in extremely handy in converting binary data to the correct formats. The easiest way to encode Base64 strings in Node.js is via the Buffer object....
🌐
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 - Base64 encoding and decoding are when you need to encode binary data into a text format that can be easily transmitted over text-based protocols like HTTP. Node.js provides built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class.
🌐
npm
npmjs.com › package › base64-arraybuffer
base64-arraybuffer - npm
January 22, 2022 - Latest version: 1.0.2, last published: 5 years ago. Start using base64-arraybuffer in your project by running `npm i base64-arraybuffer`. There are 530 other projects in the npm registry using base64-arraybuffer.
      » npm install base64-arraybuffer
    
Published: Jan 22, 2022
Version: 1.0.2
Find elsewhere
🌐
xjavascript
xjavascript.com › blog › convert-base64-string-to-arraybuffer
How to Convert Base64 String to ArrayBuffer in JavaScript: Client-Side Solution for User Input (No Server AJAX) — xjavascript.com
No server setup or external libraries are required—this is 100% client-side! Converting Base64 to ArrayBuffer involves two core steps: decoding the Base64 string to a binary string, then converting that binary string to an ArrayBuffer.
🌐
GitHub
github.com › nodejs › node › issues › 11866
buffer.write(buffer, 'base64') · Issue #11866 · nodejs/node
March 15, 2017 - What's the fastest way to convert a buffer containing base64-encoded data to a buffer containing binary data? Something like this? var base64Buffer = ...; // e.g. A buffer containing base64 provided by a mime decoder. var binaryBuffer = ...
Author: nodejs
🌐
Attacomsian
attacomsian.com › blog › nodejs-base64-encode-decode
Encoding and decoding Base64 strings in Node.js
October 1, 2022 - Internally, Buffer represents binary data in the form of a sequence of bytes. The Buffer object provides several methods to perform different encoding and decoding conversions. This includes to and from UTF-8, UCS2, Base64, ASCII, UTF-16, and even the HEX encoding scheme.
🌐
Jsonic
jsonic.io › home › guides › base64 in javascript
Base64 Encode & Decode in JavaScript — Complete Guide | Jsonic
May 11, 2026 - For UTF-8 Unicode, wrap with TextDecoder: convert the binary string from atob() to a Uint8Array, then call new TextDecoder().decode(bytes). Read the file as a Buffer and call .toString('base64'): fs.readFileSync('image.png').toString('base64'). Prepend data:image/png;base64, to create a data URI for HTML embedding. Effective TypeScript (2nd Edition) — Dan Vanderkam83 concrete ways to level up your TypeScript — inference, narrowing, and type-safe API boundaries. JavaScript: The Definitive Guide (7th Edition) — David FlanaganThe complete reference for the language JSON came from — serialization, async, and the full standard library.
🌐
GitHub
gist.github.com › indexzero › 718390
An extremely simple implementation of base64 encoding / decoding using node.js Buffers · GitHub
October 25, 2020 - return Buffer.from("Hello World").toString('base64'); and return Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('utf8');
🌐
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 - Decoding a base64-encoded string is also possible using the global Buffer class. Again, create a buffer instance using the Buffer.from method. Pass it your base64-encoded string as the first argument and the base64 encoding as the second argument. Make sure to pass the correct encoding to ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-encode-and-decode-strings-with-base64-in-javascript
How To Encode and Decode Strings with Base64 in JavaScript | DigitalOcean
Learn how to encode and decode strings with Base64 in JavaScript. This guide covers btoa(), atob(), Buffer, modern APIs, and practical examples.
🌐
Medium
medium.com › @divinehycenth8 › convert-a-base64-data-into-an-image-in-node-js-d82136576e35
Convert a Base64 data into an Image in Node.js | by Divine Hycenth | Medium
November 15, 2020 - You need to first convert your string into Buffer before saving it as a real image otherwise, you’re going to run into issues. ... Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
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 - You need to first convert your string into Buffer before saving it as a real image otherwise, you're going to run into issues. ... Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
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.
🌐
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 ...