Node.js stats.size Property from fs.Stats Class
The stats.size property is an inbuilt application programming interface of the fs.Stats class is used to get the size of the file in bytes.
Syntax:
stats.size;
Return Value: It returns a number or BigInt value that represents the size of the file in bytes.
Below examples illustrate the use of stats.size property in Node.js:
Example 1:
const fs = require( 'fs' );
fs.stat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using stat: the size the "
+ "file in bytes is " + stats.size);
});
fs.lstat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using lstat: the size the"
+ " file in bytes is " + stats.size);
});
|
Output:
using stat: the size the file in bytes is 1112
using lstat: the size the file in bytes is 1112
Example 2:
const fs = require( 'fs' ).promises;
(async () => {
const stats = await fs.stat( './filename.txt' );
console.log( "using stat synchronous: the size "
+ "the file in bytes is " + stats.size);
})(). catch (console.error)
|
Output:
(node:7040) ExperimentalWarning: The fs.promises API
is experimental
using stat synchronous: the size the file
in bytes is 1112
Note: The above program will compile and run by using the node filename.js
command and use the file_path correctly.
Reference: https://nodejs.org/api/fs.html#fs_stats_size
Last Updated :
26 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...