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

---
  title: How to check if a path is a file in Node.js?
  published: true
  tags: Nodejs
  date: Fri Dec 18 2020 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/check-if-path-is-file-nodejs
  cover_image: https://melvingeorge.me/_next/static/images/main-ebf75bf7a98c3f377517dc6e8ae5efb7.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/check-if-path-is-file-nodejs)
  
To check if a path is a file 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 `isFile()` method returned from the stats object.

```js
//  Check if path is a file
fs.statSync("file.txt").isFile();
```

Let's say we have a file called `file.txt` inside a directory called `reports`. Thus the path to the file looks like this,

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

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

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

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

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

- The first argument is the path to the file
- 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 file path
const path = "./reports/file.txt";

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

  console.log(stats);

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

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

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

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

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

  console.log(stats.isFile()); // true
});
```

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

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

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

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

It can be done like this,

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

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

// check if path is file 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 `isFile()` method to check to see if the path is a file or not like this,

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

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

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

console.log(stats.isFile()); // true
```

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

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

---