This will take filePath
as a function parameter and reads the file via fs
module and get the file and converts it's size into more humanly readable format.
const fs = require('fs');
function getFileSize(filename) {
const stats = fs.statSync(filename);
//console.log('stats', stats);
const {size} = stats;
// convert to human readable format.
const i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
}
^
fs.statSync(filePath)
will return a json object containing file stats like createdAt, modified, size, etc.