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.

🌐
npm
npmjs.com › package › nodejs-base64-encode
nodejs-base64-encode - npm
Simple Node Js Package For Encode Decode A Given String. Latest version: 1.1.0, last published: 7 years ago. Start using nodejs-base64-encode in your project by running `npm i nodejs-base64-encode`. There are 14 other projects in the npm registry using nodejs-base64-encode.
      » npm install nodejs-base64-encode
    
Published: Feb 16, 2020
Version: 1.1.0
🌐
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 - The Buffer can also be used to convert the base64 string back to utf8 encoding. 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 using the toString() method.
🌐
npm
npmjs.com › package › nodejs-base64
nodejs-base64 - npm
April 18, 2021 - The ultimate shortcut for the base64 encode/decode functions in Node.js. TypeScript types available. const { base64encode, base64decode } = require('nodejs-base64'); let encoded = base64encode('hey there'); // "aGV5ICB0aGVyZQ==" let decoded = base64decode(encoded); // "hey there"
      » npm install nodejs-base64
    
Published: Apr 18, 2021
Version: 2.0.0
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
August 22, 2017 - Decoding a Base64 string is quite similar to encoding it. You have to create a new buffer object and pass two parameters to its constructor. The first parameter is the data in Base64 and the second parameter is "base64". Then you simply have to call toString on the buffer object but this time ...
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Node.js also supports the following binary-to-text encodings. 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.
🌐
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 ...
🌐
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 - Node.js supports data encoding via the global Buffer class. You can create a buffer instance using the Buffer.from method. Pass the value you want to base64-encode as the first argument and the current encoding as the second argument.
Find elsewhere
🌐
eSparkBiz
esparkinfo.com › base64 encoding in node.js
Base64 Encoding in Node.js Using Built-in Buffer API
June 5, 2026 - Base64 encode and decode strings or files using Node.js’s Buffer class.
🌐
npm
npmjs.com › package › base-64
base-64 - npm
October 17, 2020 - This function takes a byte string (the input parameter) and encodes it according to base64. The input data must be in the form of a string containing only characters in the range from U+0000 to U+00FF, each representing a binary byte with values ...
      » npm install base-64
    
Published: Oct 17, 2020
Version: 1.0.0
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › solved: how to do nodejs base64 encode & decode
SOLVED: How to do Nodejs Base64 Encode & Decode | GoLinuxCloud
September 7, 2022 - You can pipe a string to the base64 command on the terminal. ... For example, we can encode "Great" as follows. ... The result is R3JlYXQK. Conversely, we can decode the encoded data using the --decode option. ... For example, we can decode "R3JlYXQK" as follows. ... The result is Great. Although you can encode data on the terminal, using a runtime environment or a programming language and a code editor simplifies handling complex data. Let us explore that using Nodejs base64 encode.
🌐
GitHub
github.com › localtools › nodejs-base64
GitHub - localtools/nodejs-base64: Node.js Base64 Text Encoder/Decoder · GitHub
The encode method takes an object with a text field, and returns the Base64 encoding of the text.
Author: localtools
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › node-js-base64-encoding-and-decoding
Node.js - Buffer.toString() Method
August 16, 2021 - Find the perfect tutorial for your learning journey · The NodeJS Buffer.toString() method helps to decode the string as per the encoding given. By default, the encoding used is 'utf-8'
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-nodejs
Base64 with NodeJS | Binary Encoding Techniques Across Programming Languages
Always be explicit about the input ... when creating the buffer. Node.js makes decoding Base64 straightforward using the built-in Buffer object....
🌐
Colbyhemond
colbyhemond.me › blog › encoding-base64
Encoding Base64 with Node.js | Blog - Colby Hemond
June 13, 2022 - const base64String = ... large binary data like video, audio, or streamed content. It lets you encode/decode and manipulate data at the byte level....
🌐
GitHub
gist.github.com › tatsuyasusukida › ce71456081748242a0bd4cbfcfe44eb7
⚾️ How to encode / decode Base64URL with Node.js [video version available]
The encodeBase64Url function replaces "+" with "-", "/" with "_", and removes =. The decodeBase64Url function first replaces "-" with "+" and "_" with "/". Then use the Buffer.from function to convert the Base64 string to Buffer.
🌐
UsefulAngle
usefulangle.com › post › 303 › nodejs-base64-encode-decode
Base64 Encoding and Decoding in Node.JS - UsefulAngle
February 1, 2020 - Node.js does not support the standard Javascript methods of atob() and btoa() for base64 conversions. Encoding and decoding base64 data in Node can be done through the inbuilt Buffer module.