How to get filesize in Node.js


You need to get the size of a local file using Node.js


Assuming you want to get the size of myfile.txt need to 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