Node.js - check if path is file

3 points
Created by:

In this article, we would like to show you how to check if path is a file using Node.js.

Quick solution:

const fs = require('fs');

const status = fs.lstatSync('/path/to/somewhere');

if (status.isFile()) {
    // ...


When /path/to/somewhere is soft link and indicates to non-existing path, it is necessary to call fs.existsSync(/path/to/somewhere) before status checking.


Practical example

Projects structure:

  +-- my_directory/
  |    |
  |    +-- my_file.json
  +-- my_script.js

Example my_script.js file:

const fs = require('fs');

const isFile = (path) => {
    const status = fs.lstatSync(path);
    return status.isFile();

// Usage example:

console.log(isFile('my_directory'));              // false
console.log(isFile('my_directory/my_file.txt'));  // true



  1. callbacks based example
  2. async/await based example


Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - file system module

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂