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
.