How to check if a path is a directory in Node.js?
Tags: node
Published: 2020-12-19 00:00:00

---
  title: How to check if a path is a directory in Node.js?
  published: true
  tags: Nodejs
  date: Sat Dec 19 2020 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/check-if-path-is-directory-nodejs
  cover_image: https://melvingeorge.me/_next/static/images/main-6410b5c7b31f16b99bf070e65d933fb3.jpg
  ---
> [Originally posted here!](https://melvingeorge.me/blog/check-if-path-is-directory-nodejs)
  
To check if a path is a directory in Node.js, we can use the `stat()` (asynchronous execution) function or the `statSync()` (synchronous execution) function from the `fs` (filesystem) module and then use the `isDirectory()` method returned from the stats object.

```js
// Check if path is a directory
fs.statSync("./reports").isDirectory();
```

For example, Let's say we have a directory called `reports` so that the path to the directory looks like this,

```js
// require fs module
const fs = require("fs");

// a path
const path = "./reports";
```

## Using the `fs.stat()` function (Asynchronous Way)

Let's check if the path is a directory using the `fs.stat()` function.

The `fs.stat()` function needs 2 arguments:

- The first argument is the path to check
- The second argument is an error first callback function that will get executed after reading all the stats about the path.

It can be done like this,

```js
// require fs module
const fs = require("fs");

// a path
const path = "./reports";

// check if path is directory using
// the fs.stat() function
fs.stat(path, (error, stats) => {
  // incase of error
  if (error) {
    console.error(error);
    return;
  }

  console.log(stats);

  /*
    Stats {
        dev: 3822,
        mode: 16877,
        nlink: 1,
        uid: 1000,
        gid: 1000,
        ..
        ..
    }
    */
});
```

In the `stats` object we can use the `isDirectory()` method to check to see if the path is a directory or not like this,

```js
// require fs module
const fs = require("fs");

// a path
const path = "./reports";

// check if path is directory using
// the fs.stat() function
fs.stat(path, (error, stats) => {
  // incase of error
  if (error) {
    console.error(error);
    return;
  }

  // check if path is a directory
  console.log(stats.isDirectory()); // true
});
```

See this example live in [repl.it](https://repl.it/@melvin2016/check-if-path-is-directory-nodejs#index.js).

## Using the `fs.statSync()` function (Synchronous Way)

Let's check if the path is a directory using the `fs.statSync()` function.

The `fs.statSync()` function needs a path to check as the first argument.

It can be done like this,

```js
// require fs module
const fs = require("fs");

// a path
const path = "./reports";

// check if path is directory using
// the fs.statSync() function
const stats = fs.statSync(path);
```

The function returns an object containing all the stats about the path.

In the `stats` object we can use the `isDirectory()` method to check to see if the path is a directory or not like this,

```js
// require fs module
const fs = require("fs");

// a path
const path = "./reports";

// check if path is directory using
// the fs.statSync() function
const stats = fs.statSync(path);

// check if directory
console.log(stats.isDirectory()); // true
```

See this example live in [repl.it](https://repl.it/@melvin2016/check-if-path-is-directory-nodejs#index.js).

### Feel free to share if you found this useful 😃.

---