Skip to content

Instantly share code, notes, and snippets.

@DiegoVictor
Created December 1, 2021 23:57
Show Gist options
  • Save DiegoVictor/81e758af99bd7d414eb976fe1ce2945d to your computer and use it in GitHub Desktop.
Save DiegoVictor/81e758af99bd7d414eb976fe1ce2945d to your computer and use it in GitHub Desktop.
Convert image to base64 using Node.js' native libs
// Usage: node converter.js <file path>
const {
promises: { readFile, writeFile },
} = require("fs");
const [, , filePath] = process.argv;
async function converter(path) {
return readFile(path)
.then((buffer) =>
writeFile(
"base64.txt",
buffer.toString("base64")
)
);
}
converter(filePath).then(() => {
process.stdout.write(`\nOutput:\n${__dirname}\\base64.txt\n`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment