btoa is not defined error in node js | buffer in node js

The btoa() function takes a JavaScript string as a parameter. In JavaScript strings are represented using the UTF-16 character encoding: in this encoding, strings are represented as a sequence of 16-bit (2 bytes) units.

But bota() function is not available in node js to achieve the same functionality we can use the buffer in node js.

// Try in browser console
btoa('Hello india') // "SGVsbG8gaW5kaWE="

The same result we can achieve with the help of node Js buffer.

Buffer.from('Hello india').toString('base64') // SGVsbG8gaW5kaWE=

What is buffer?

A buffer is an area of memory. It represents a fixed-size chunk of memory (can’t be resized) allocated outside of the V8 JavaScript engine. You can think of a buffer like an array of integers, which each represent a byte of data.

Buffers were introduced to help developers deal with binary data, in an ecosystem that traditionally only dealt with strings rather than binaries.

How to create a buffer

A buffer is created using the Buffer.from()Buffer.alloc(), and Buffer.allocUnsafe() methods.

Access the content of a buffer

A buffer, being an array of bytes, can be accessed like an array:

const buf = Buffer.from('Hi !!')
console.log(buf) // <Buffer 48 69 20 21 21>
console.log(buf[0]) //72
console.log(buf[1]) //105
console.log(buf[2]) //32
console.log(buf[3]) //33

Those numbers are the UTF-8 bytes that identify the characters in the buffer (H → 72, i → 105, ! → 33). This happens because Buffer.from() uses UTF-8 by default. Keep in mind that some characters may occupy more than one byte in the buffer (é → 195 169).

You can print complete strings using toString method.

console.log(buf.toString()); // Hi !!

Get length of buffer

const buf = Buffer.from('Hi !!');
console.log(buf.length); // 5

Iterate over the contents of a buffer

const buf = Buffer.from('Hi !!')
for (const item of buf) {
  console.log(item) //72 105 32 33
}

Slice a buffer

If you want to create a partial visualization of a buffer, you can create a slice. Use the subarray() method to create it. The first parameter is the starting position, and you can specify an optional second parameter with the end position.

const buf = Buffer.from('Hey!')
buf.subarray(0).toString() //Hey!
const slice = buf.subarray(0, 2)
console.log(slice.toString()) //He
buf[1] = 111 //o
console.log(slice.toString()) //Ho

Copy a buffer

Using set() method we can copy a buffer.

const buf = Buffer.from('Hi !!')
let bufcopy = Buffer.alloc(5) //allocate 5 bytes
bufcopy.set(buf)

By default, you copy the whole buffer. If you only want to copy a part of the buffer, you can use .subarray() and the offset argument that specifies an offset to write to:

const buf = Buffer.from('Hey?')
let bufcopy = Buffer.from('Moo!')
bufcopy.set(buf.subarray(1, 3), 1)
bufcopy.toString() //'Mey!'
What is the buffer in nodejs?

A buffer is an area of memory. It represents a fixed-size chunk of memory (can’t be resized) allocated outside of the V8 JavaScript engine

How use btoa in nodejs?

btoa‘ is not available in node js but you can use Buffer.from() to achieve a similar result. example: Buffer.from('Hi').toString('base64'))

Leave a Reply