How to get filesize in Node.js

To determine the size of a file in NodeJS (e.g. to get the size of myfile.txt) use fs.stat() or fs.statSync() like this:

const fs = require("fs"); //Load the filesystem module
const stats = fs.statSync("myfile.txt");
const fileSizeInBytes = stats.size;
//Convert the file size to megabytes (optional)
const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;

Another option is to use the following function:

function getFilesizeInBytes(filename) {
    const stats = fs.statSync(filename);
    const fileSizeInBytes = stats.size;
    return fileSizeInBytes;
}