EN
Node.js - btoa is not defined error
1
answers
0
points
I tried to encode text to base64 using btoa
function in Node.js, but I get this error:
const base64Data = btoa(data);
^
ReferenceError: btoa is not defined
My code:
const data = 'password';
const base64Data = btoa(data);
console.log(base64Data);
How to solve it? Is there no btoa method in Node.js?
1 answer
0
points
Unfortunately, Node.js does not support this method. The solution, however, is to use the Buffer
class, so you can use:
const btoa = (text) => {
return Buffer.from(text, 'binary').toString('base64');
};
console.log(btoa('This is text...')); // VGhpcyBpcyB0ZXh0Li4u
Check out this article:
0 comments
Add comment