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
🌐
npm
npmjs.com › package › base-64
base-64 - npm
October 17, 2020 - A robust base64 encoder/decoder that is fully compatible with `atob()` and `btoa()`, written in JavaScript.. Latest version: 1.0.0, last published: 6 years ago. Start using base-64 in your project by running `npm i base-64`. There are 1777 other ...
      » npm install base-64
    
Published: Oct 17, 2020
Version: 1.0.0
🌐
npm
npmjs.com › package › js-base64
js-base64 - npm
August 17, 2026 - Yet another Base64 transcoder in pure-JS. Latest version: 3.9.4, last published: 10 days ago. Start using js-base64 in your project by running `npm i js-base64`. There are 4094 other projects in the npm registry using js-base64.
      » npm install js-base64
    
Published: Sep 19, 2026
Version: 3.9.4
🌐
npm
npmjs.com › package › Base64
Base64 - npm
Base64 encoding and decoding. Latest version: 1.3.0, last published: 3 years ago. Start using Base64 in your project by running `npm i Base64`. There are 156 other projects in the npm registry using Base64.
      » npm install Base64
    
Published: Sep 18, 2023
Version: 1.3.0
🌐
npm
npmjs.com › package › base64-js
base64-js - npm
November 11, 2020 - Base64 encoding/decoding in pure JS. Latest version: 1.5.1, last published: 6 years ago. Start using base64-js in your project by running `npm i base64-js`. There are 3297 other projects in the npm registry using base64-js.
      » npm install base64-js
    
Published: Nov 11, 2020
Version: 1.5.1
🌐
npm
npmjs.com › package › nodejs-base64
nodejs-base64 - npm
April 18, 2021 - Latest version: 2.0.0, last published: 5 years ago. Start using nodejs-base64 in your project by running `npm i nodejs-base64`. There are 55 other projects in the npm registry using nodejs-base64.
      » npm install nodejs-base64
    
Published: Apr 18, 2021
Version: 2.0.0
Starred by 5 users
Forked by 2 users
Languages: JavaScript
🌐
npm
npmjs.com › package › react-native-base64
react-native-base64 - npm
September 21, 2025 - Base64 encoding and decoding helping util. Created for React Native but can be used anywhere. Latest version: 0.2.2, last published: a year ago. Start using react-native-base64 in your project by running `npm i react-native-base64`. There are 121 other projects in the npm registry using react-native-base64.
      » npm install react-native-base64
    
Published: Sep 21, 2025
Version: 0.2.2
🌐
npm
npmjs.com › package › hi-base64
hi-base64 - npm
A simple Base64 encode / decode function for JavaScript supports UTF-8 encoding.. Latest version: 0.4.1, last published: a year ago. Start using hi-base64 in your project by running `npm i hi-base64`. There are 9 other projects in the npm registry ...
      » npm install hi-base64
    
Published: Oct 02, 2024
Version: 0.4.1
Author: Chen, Yi-Cyuan
🌐
npm
npmjs.com › package › base64-string
base64-string - npm
May 10, 2024 - Encode or decode base64 strings - including cli. Latest version: 1.1.3, last published: 2 years ago. Start using base64-string in your project by running `npm i base64-string`. There are 12 other projects in the npm registry using base64-string.
      » npm install base64-string
    
Published: May 10, 2024
Version: 1.1.3
Find elsewhere
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 › is-base64
is-base64 - npm
March 4, 2020 - Predicate that returns true if base64 string.. Latest version: 1.1.0, last published: 6 years ago. Start using is-base64 in your project by running `npm i is-base64`. There are 740 other projects in the npm registry using is-base64.
      » npm install is-base64
    
Published: Mar 04, 2020
Version: 1.1.0
🌐
npm
npmjs.com › package › @types › base-64
@types/base-64 - npm
TypeScript definitions for base-64. Latest version: 1.0.2, last published: 2 years ago. Start using @types/base-64 in your project by running `npm i @types/base-64`. There are 78 other projects in the npm registry using @types/base-64.
      » npm install @types/base-64
    
🌐
npm
npmjs.com › package › @jsonjoy.com › base64
@jsonjoy.com/base64 - npm
May 15, 2026 - Fast Base64 encoder and decoder for browser and Node.js.
      » npm install @jsonjoy.com/base64
    
Published: Jun 02, 2026
Version: 18.28.0
🌐
npm
npmjs.com › package › @hexagon › base64
@hexagon/base64 - npm
Encode, decode and validate base64/base64url to string/arraybuffer and vice-versa.
      » npm install @hexagon/base64
    
Published: Oct 18, 2024
Version: 2.0.4
🌐
npm
npmjs.com › package › base64-encoding
base64-encoding - npm
Latest version: 0.15.0-alpha.0, last published: 4 years ago. Start using base64-encoding in your project by running `npm i base64-encoding`. There are 3 other projects in the npm registry using base64-encoding.
      » npm install base64-encoding
    
Published: Apr 05, 2022
Version: 0.15.0-alpha.0
Author: Florian Klampfer
🌐
npm
npmjs.com › search
base64 - npm search
A C++ module for node-js that does base64 encoding and decoding.
🌐
npm
npmjs.com › package › @juanelas › base64
@juanelas/base64 - npm
Base64 for both node.js and browser javascript. It supports URL-safe encoding and enabling/disabling padding. Buffers can be implementedd using ArrayBuffer, any TypedArray, or Buffer. Latest version: 1.1.5, last published: 3 years ago. Start using @juanelas/base64 in your project by running `npm i @juanelas/base64`. There are 11 other projects in the npm registry using @juanelas/base64.
      » npm install @juanelas/base64
    
Published: Nov 10, 2023
Version: 1.1.5
🌐
npm
npmjs.com › package › nodejs-base64-encode
nodejs-base64-encode - npm
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
🌐
npm
npmjs.com › package › @loxjs › node-base64
@loxjs/node-base64 - npm
A Node.js module that provides URL-safe Base64 encoding and decoding.. Latest version: 2.0.3, last published: 3 years ago. Start using @loxjs/node-base64 in your project by running `npm i @loxjs/node-base64`. There are no other projects in the npm registry using @loxjs/node-base64.
      » npm install @loxjs/node-base64
    
Published: Mar 07, 2024
Version: 2.0.3
🌐
Npm
npm.io › search › keyword:base64
Base64 | npm.io
A robust base64 encoder/decoder that is fully compatible with `atob()` and `btoa()`, written in JavaScript.