How to check the given path is file or directory in node.js ?
Last Updated :
25 Apr, 2025
Sometimes there is a need to check whether the given path is a 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 the fs module. You can read more about it here. We can check the path for files or directories in Node.js in both Synchronous and Asynchronous ways.
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 a synchronous mode in the fs module, we can use the 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 the file path is Directory, otherwise returns false.
Example 1: In this example, we will see the use of the Use statSync() method to store the returned instance into a variable named stats.
javascript
// Require the given module
const fs = require('fs');
// Use statSync() method to store the returned
// instance into variable named stats
let stats = fs.statSync(
"/Users/divyarani/Documents/geekforgeeks/geeks.js");
// Use isFile() method to log the result to screen
console.log('is file ? ' + stats.isFile());
stats = fs.statSync(
"/Users/divyarani/Documents/geekforgeeks/geek");
// Use isDirectory() method to log the result to screen
console.log('is directory ? ' + stats.isDirectory());
Output:
is file ? true
is directory ? true
Example 2: In this example, we will see the use of the Use statSync() method to store the returned instance into a variable named stats.
javascript
// Require the given module
const fs = require('fs');
// Use statSync() method to store the returned
// instance into variable named stats
let stats = fs.statSync(
"/Users/divyarani/Documents/geekforgeeks/geek");
// Use isFile() method to log the result to the screen
console.log('is file ? ' + stats.isFile());
stats = fs.statSync(
"/Users/divyarani/Documents/geekforgeeks/geeks.js");
// Use isDirectory() method to log the result to screen
console.log('is directory ? ' + stats.isDirectory());
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 the 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, the first parameter is the path and the second is the callback function with two parameters, one is for error in case an 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 the file path is Directory, otherwise returns false.Â
Example 1: In this example, we will see the use of the stat() method.
javascript
// Require the given module
const 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;
});
Output:
is directory? true
Example 2: In this example, we will see the use of the stat() method.
javascript
// Require the given module
const 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;
});
Output:
is file ? true
Similar Reads
How to get the current pathname in the app directory of Next.js?
NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to get the current pathname in the ap
3 min read
How to Access the File System in Node.js ?
In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files. Prerequisite: Basic knowledge of ES6Basic knowledge of NodeJS NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses
3 min read
How to Get File Character Encoding in Node.js ?
Character encoding is essential when working with text files, as it determines how characters are represented in bytes. Different files may use different encodings, such as UTF-8, ASCII, or ISO-8859-1. Determining the character encoding of a file in Node.js can help ensure proper reading and process
2 min read
How to check a function is defined in JavaScript ?
In this article, we will check whether a function is defined or not in JavaScript. The JavaScript typeof operator is used to check whether the function is defined or not. JavaScript typeof OperatorThe typeof operator is used to find the type of a JavaScript variable. This operator returns the type o
2 min read
How to determine if Node.js process is running in a git directory ?
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment. It allows developers to run JavaScript on the server side, creating server-side applications with JavaScript. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for
5 min read
How to filter path of routes in Express.js ?
Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will be discussing how to filter the paths of routes using the express.js in node.js. The ap
2 min read
How to Copy a File in Node.js?
Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How to Get the Path of Current Script using Node.js ?
In Node JS, getting the path of the current script is useful for file operations, logging, and configuration. It allows you to access related files or directories reliably, regardless of where your Node.js process was started.ApproachTo get the path of the present script in node.js we will be using
2 min read
Shell Script to Check if Every Passed Argument is a File or Directory
While automating things in Linux we always work on the file and directories. And sometimes we pass the files and directories as the arguments to the shell scripts. And we have to determine whether the provided argument is a file or a directory, and today we are going to see how to check whether prov
4 min read
How to have path alias in Node.js ?
Node.js has tons of modules that can be very hard to deal with. Thus, it's a good practice to have a neat and well-defined directory structure. In this article, we are going to deal with a problem that involves the Node.js modules. Â Let's first understand what is the need to have path alias. There a
3 min read