How to check the given path is file or directory in node.js ?

Sometimes there is a need to check whether the given path is file or directory so that different operations can be performed based on the result. For instance, to log the information of the directory and file separately.
In Node.js, file handling is handled by fs module. You can read more about it here. We can check the path for file or directory in Node.js in both Synchronous and Asynchronous way.

Note: Asynchronous version is usually preferable if you care about application performance.

Synchronous method: Synchronous operations are great for performing one-time file/directory operations before returning a module. To check the path in synchronous mode in fs module, we can use statSync() method. The fs.statSync(path) method returns the instance of fs.Stats which is assigned to variable stats. A fs.Stats object provides information about a file. The stats.isFile() method returns true if the file path is File, otherwise returns false. The stats.isDirectory() method returns true if file path is Directory, otherwise returns false.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require the given module 
var fs = require('fs');
   
// Use statSync() method to store the returned
// instance into variable named stats
var stats = fs.statSync("/Users/divyarani/Documents/geekforgeeks/geeks.js");
   
// Use isFile() method to log the result to screen
console.log('is file ? ' + stats.isFile());
  
var stats = fs.statSync("/Users/divyarani/Documents/geekforgeeks/geek");
   
// Use isDirectory() method to log the result to screen
console.log('is directory ? ' + stats.isDirectory());

chevron_right


Output:



is file ? true
is directory ? true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require the given module 
var fs = require('fs');
   
// Use statSync() method to store the returned
// instance into variable named stats
var stats = fs.statSync("/Users/divyarani/Documents/geekforgeeks/geek");
   
// Use isFile() method to log the result to the screen
console.log('is file ? ' + stats.isFile());
    
var stats = fs.statSync("/Users/divyarani/Documents/geekforgeeks/geeks.js");
   
// Use isDirectory() method to log the result to screen
console.log('is directory ? ' + stats.isDirectory());

chevron_right


Output:

is file ? false
is directory ? false

Asynchronous method: In the Asynchronous version, the code block within the function will be mostly non-blocking to the end-user and user will not be prevented to perform different actions for various sub-processes. To check the path in asynchronous mode in the fs module we can use fs.stat() method. The fs.stat() method takes two parameters, first parameter is the path and the second is the callback function with two parameters, one is for error in case error occurred and the second parameter is the data retrieved by calling fs.stat() method which is stored in stats variable. The stats.isFile() method returns true if the file path is File, otherwise returns false. The stats.isDirectory() method returns true if file path is Directory, otherwise returns false.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require the given module 
var fs = require('fs'),
path = "/Users/divyarani/Documents/geekforgeeks/geek"
   
// Use stat() method
fs.stat(path, (err, stats) => {
    if( !err ){
         if(stats.isFile()){
             console.log('is file ? ' + stats.isFile());
         }
   
         else if(stats.isDirectory()){
             console.log('is directory? ' + stats.isDirectory());
         }
     }
     else
         throw err; 
});

chevron_right


Output:

is directory? true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require the given module
var fs = require('fs'),
path = "/Users/divyarani/Documents/geekforgeeks/geeks.js"
  
// Use stat() method
fs.stat(path, (err, stats) => {
  if( !err ){
       if(stats.isFile()){
           console.log('is file ? ' + stats.isFile());
       }
  
       else if(stats.isDirectory()){
           console.log('is directory? ' + stats.isDirectory());
       }
   }
   else
      throw err; 
});

chevron_right


Output:

is file ? true



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.