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:
// Node.js program to demonstrate the // stats.size property // Accessing fs module const fs = require( 'fs' );
// Calling fs.Stats stats.size // for files using stat fs.stat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using stat: the size the "
+ "file in bytes is " + stats.size);
}); // Using lstat 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:
// Node.js program to demonstrate the // stats.size property // Accessing fs module const fs = require( 'fs' ).promises;
// Calling fs.Stats stats.size (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