Skip to content
Related Articles

Related Articles

Improve Article

Node.js stats.isDirectory() Method from fs.Stats Class

  • Last Updated : 29 Jun, 2020

The stats.isDirectory() method is an inbuilt application programming interface of the fs.Stats class which is used to check whether fs.Stats object describes a file system directory or not.

Syntax:

stats.isDirectory();

Parameters: This method does not accept any parameters.

Return: This method returns a boolean value, which is true if fs.Stats object describes a directory, false otherwise.

Below examples illustrate the use of stats.isDirectory() method in Node.js:



Example 1:




// Node.js program to demonstrate the   
// stats.isDirectory() Method
  
// Accessing fs module
const fs = require('fs');
  
// Calling fs.Stats isDirectory()
fs.stat('./filename.txt', (err, stats) => {
    if (err) throw err;
    // console.log(`stats: ${JSON.stringify(stats)}`);
    console.log(stats.isDirectory());
});
  
  
fs.stat('./filename.txt', (err, stats) => {
    if (err) throw err;
    // console.log(`stats: ${JSON.stringify(stats)}`);
  
    if (stats.isDirectory()) {
        console.log("fs.Stats describes a "
            + "file system directory");
    } else {
        console.log("fs.Stats does not "
        + "describe a file system directory");
    }
});

Output:

false
fs.Stats does not describe a file system directory

Example 2:




// Node.js program to demonstrate the   
// stats.isDirectory() Method
  
// Accessing fs module
const fs = require('fs');
  
// calling fs.Stats isDirectory()
fs.stat('./', (err, stats) => {
    if (err) throw err;
    // console.log(`stats: ${JSON.stringify(stats)}`);
    console.log(stats.isDirectory());
});
  
fs.stat('./', (err, stats) => {
    if (err) throw err;
    // console.log(`stats: ${JSON.stringify(stats)}`);
    if (stats.isDirectory()) {
        console.log("fs.Stats describes a "
            + "file system directory");
    } else {
        console.log("fs.Stats does not "
        + "describe a file system directory");
    }
});

Output:

true
fs.Stats describes a file system directory

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_isdirectory




My Personal Notes arrow_drop_up
Recommended Articles
Page :