Created
December 29, 2023 20:47
-
-
Save PatrickJS/dcb96a7cbe25a57ea5c7283794d6682a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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