How to convert a string to base64 in Node.js

While making a Shopify App with Node.js, I had to convert a string into base64.

Initially, I tried to use the btoa() function available in JavaScript, but I got a “btoa is not defined” error in Node.js. Turns out it’s not available.

So if you want to encode a string into base64 in Node.js, do the following instead:

Buffer.from('YOUR STRING HERE').toString('base64')

That will give you back the base64 version.

Looking to learn how to code and make money online? Check out CodingPhase (referral link)