Categories
JavaScript Answers

How to find the size of the file in Node.js?

Spread the love

To find the size of the file in Node.js, we use the statSync method.

For instance, we write

const fs = require("fs");

const stats = fs.statSync("myfile.txt");
const fileSizeInBytes = stats.size;
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);

to call statSync to get the stats for the myfile.txt file.

We get the file size in bytes with stats.size.

Then we convert it to megabytes by dividing it by 1024 * 1024.

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 *