Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created December 29, 2023 20:47
Show Gist options
  • Select an option

  • Save PatrickJS/dcb96a7cbe25a57ea5c7283794d6682a to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/dcb96a7cbe25a57ea5c7283794d6682a to your computer and use it in GitHub Desktop.
const fs = require('fs');
// Function to get a file size
function getFileSize(filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if(err) {
reject(err);
} else {
let fileSizeInBytes = stats.size; // size in bytes
resolve(fileSizeInBytes.toString()); // convert size to string
}
});
});
}
// Usage
getFileSize('./your/file/path')
.then(size => console.log(size))
.catch(err => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment