nodejs base64 to image code example

Example 1: nodejs read image as base64

const fs = require('fs');
const contents = fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'});

Example 2: nodejs fs writefile base64url

const fileContents = new Buffer(attachmentResponse.data.data, 'base64')
fs.writeFile(part.filename, fileContents, (err) => {
  if (err) return console.error(err)
  console.log('file saved to ', part.filename)
})

Example 3: nodejs read image as base64

const fs = require('fs').promises;
const contents = await fs.readFile('/path/to/file.jpg', {encoding: 'base64'});

Example 4: node.js - How do I convert an image to a base64-encoded data URL

var fs = require('fs');

var imageAsBase64 = fs.readFileSync('./your-image.png', 'base64');