| by Arround The Web | No comments

How to Use stats.isDirectory() Method in Node.js?

The Node.js “fs(File System)” built-in module is used to interact and manipulate the operating system files or folders in a way of accessing, searching, updating, renaming, and removing them. Moreover, it also allows the users to fetch the details of the system’s files or folders. This module performs these particular operations with the help of its pre-defined synchronous and asynchronous methods such as “fs.access()”, “fs.accessSync()”, “fs.stat()”, “fs.statSync()”, “stats.isFile()”, “stats.isDirectory()” and many others.

This write-up will demonstrate the working of the “stats.isDirectory()” in Node.js.

How Does “stats.isDirectory()” Method Work in Nodejs?

The “isDirectory()” is the predefined method of the “fs.Stat” class that checks whether the “fs.Stats” object specifies a file system directory or not. The “fs.Stats” object follows some built-in properties and methods that get the details of the particular file/folder based on their names and functionalities.

Syntax

The working of the “stats.isDirectory()” method depends on its generalized syntax which is written here:

stats.isDirectory();

 
According to the above syntax, the “stats.isDirectory()” method does not require any additional parameters to perform its defined task.

Return Values: This method provides a “boolean” value “true” if the “fs.Stats” object describes a directory otherwise “false”.

Now, see the practical implementation of the above-defined method.

Example 1: Applying “stats.isDirectory()” Method

This example utilizes the “stats.isDirectory()” method to check whether the “fs.Stats” object describes a directory or not:

const fs = require("fs");
fs.stat("./hello", function(error, stats){
if(error){
 console.error(error)
}else{
console.log(stats.isDirectory())
}
});

 
In the above lines of code:

    • Firstly, the “require()” method imports the “fs(File System)” module into the current Node.js project.
    • Next, the “fs.stat()” method passes the desired directory name and path as the first parameter and the callback function with the “error” and “stats” arguments as the second parameter.
    • After that, the callback function defines an “if-else” statement. If an error occurs, then the “if” code block will execute displaying that error message using the “console.error()” method.
    • On the other hand, if any error is not generated, then the “else” statement will be executed that contains the “console.log()” method in which the “stats” parameter is concatenated with the “isDirectory()” method to check whether the returned “fs.Stats” object is a directory or not.

Note: Create a “.js” file of any name and write the above code lines into it. For instance, we have created “app.js”.

Output

Initiate the “app.js” file by executing the below-stated command:

node app.js

 
The following output contains a “true” boolean value as a result that shows the returned “fs.Stats” object describes a directory:


Example 2: Applying “stats.isDirectory()” With “fs.statSync()” Method

This example uses the “fs.statSync()” method to retrieve the information of the specified directory synchronously and also applies the “stats.isDirectory()” to check whether the specified path is a directory or not:

const fs = require("fs");
fs.statSync("./hello", function(error, stats){
if(error){
 console.error(error)
}else{
console.log("Path is a directory: " + stats.isDirectory());
console.log(stats)
}
});

 
In the above code snippet:

    • The “fs.statsSync()” method retrieves the specified directory statistics synchronously.
    • The “console.log()” with the “stats” parameter displays the given directory statistics in the console.
    • The remaining code block is the same as in example 1.

Output

Execute the “app.js” file:

node app.js

 
The below output first shows that the specified path is a directory and then displays its statistics:


 

That’s all about working on the “stats.isDirectory()” in Node.js.

Conclusion

The Node.js “stats.isDirectory()” method works on the filesystem directories by checking whether the returned “fs.Stats” object specifies the directory or not. Its working relies on its basic syntax that does not support any additional parameter to perform the defined task. Moreover, it can be utilized with other methods to perform the additional functionality as per requirements. This post has practically explained the working of the “stats.isFile()” in Node.js.

Share Button

Source: linuxhint.com

Leave a Reply