Published on

How to Read a File as Buffer in Node.js


When we work with file systems in Node.js, we often use buffer to work with files which gives us much more flexibility with standard API.

Here is how to read a file as a buffer in Node.js

import fs from 'fs/promises';

const filePath = 'path/to/file'

// Promise-based code

fs.readFile(filePath).then(fileBuffer => {
    // your logic here

// async/await code
const fileBuffer = await fs.readFile(filePath);

Happy reading buffers!