Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *