How to fix Node.js throwing “btoa is not defined” error?

Sometimes, we want to fix Node.js throwing the “btoa is not defined” error.

In this article, we’ll look at how to fix Node.js throwing the “btoa is not defined” error.

How to fix Node.js throwing “btoa is not defined” error?

To fix Node.js throwing the “btoa is not defined” error, we can use the Buffer.from method with toString instead of btoa.

For instance, we write

console.log(Buffer.from('Hello World').toString('base64'));

to call Buffer.from with the string that we want to convert to base64.

Then we call toString with 'base64' to convert it to a base64 string.

We can decode it with

console.log(Buffer.from(b64Encoded, 'base64').toString());

where b64Encoded is the base64 string to decode.

Conclusion

To fix Node.js throwing the “btoa is not defined” error, we can use the Buffer.from method with toString instead of btoa.