What is fs.stat() in Node.js?

Node.js libraries provide a number of ways to interact with our file system and process them in several ways. We use Node’s fs, file system module, to manipulate files on our devices for this very purpose. We can create, read, update, rename, and delete files and directories with the fs module. We can also retrieve specific information about files and directories. For this purpose, the file system module provides the fs.stat() method. Let’s discuss how we can use fs.stat() with both callbacks and promises.

Note: To get started with the fs module, we need to install Node.js first. Refer to the How to install Node JS on your system Answer for help with the installation.

The fs.stat method

The stat method in the fs module allows us to fetch details about any file regardless of the file type. All we need to do is specify the path leading to that file, and once the method loads the file details, a callback function will be returned. We’ll pass that callback function two arguments: first, an error message, and, second, an object containing the file stats.

Syntax

The following snippet shows the general syntax for using this method. There are two ways of using fs.stat(), one which returns a callback and the other which returns a promise.

fs.stat(file_path, options, call_back)
The fs.stat() parameters

Let’s take a look at each parameter:

  • file_path: This is the path to the file or directory whose stats we require.

  • options: To specify any optional arguments, we use the options object. The only argument that can be omitted from the option object is bigint—it specifies whether numeric values returned by fs.stat() are bigint.

  • call_back: As the name suggests, this is a callback function that executes after fs.stat().

With callbacks

After reading the file, a callback is returned, passing the error message and file stats as arguments. The my_file_stats.size method returns the file’s size.

const fs = require("node:fs");
const file_path = "my_file.txt"
fs.stat( file_path, (error, my_file_stats) => {
if (!error){
console.log(my_file_stats.size);
}
else{
console.error("Error occurred : ", error);
}
})
Syntax example for the fs.stat() method that returns a callback

With promises

The following method will handle promises, but the rest of the procedures for retrieving file-related information will remain the same.

const fs = require("node:fs/promises");
const file_path = "my_file.txt"
async function retrieve_file_data(){
try{
const my_file_stats = await fs.stat(file_path)
console.log(my_file_stats.size);
}
catch(error){
console.error("Error occurred : ", error);
}
}
retrieve_file_data()
Syntax example for the fs.stat() method that returns a promise

Working example

The following example shows how to use the fs.stat() method. We’ll fetch the file details, like file size, and whether it is a file, directory, or symlink for three types of files: a text file, a spreadsheet, and an image file. Run the code below. Explore and make any desired changes.

hello educative users
Code example for the fs.stat() method

Code explanation

Lines 1–2: First, we acquire the fs library for calling fs.stat() through simple callbacks or promises.

Lines 8–34: Then, we call the fs.stat() method in a way described in the callback section for both text and spreadsheet files. We just need to specify the file path leading to the text file. Lastly, we call the stats methods, namely:

  • my_text_file_stats.isFile(): This method checks whether the path given is of a file.

  • my_text_file_stats.isDirectory(): The isDirectory() method checks whether the specified path leads to a directory or not.

  • my_text_file_stats.isSymbolicLink(): This method returns true if the path provided is that of a symbolic link.

  • my_text_file_stats.size: This method returns the size of the file.

Lines 36–50: Using the promises way, we apply the fs.stat() method for an image file and a directory.

Conclusion

In conclusion, the fs.stat() method returns any file or directory-related information, whether the specified path is that of a file, directory, or symbolic link, and its size as well.

This method checks whether the path given is of a file.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved