The Wayback Machine - https://web.archive.org/web/20240204104601/https://www.geeksforgeeks.org/node-js-stats-size-property-from-fs-stats-class/
Open In App
Related Articles

Node.js stats.size Property from fs.Stats Class

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 26 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials