How To Check If Path Is File Or Directory Using Node.js

By Yashwant Chavan, Views 39526, Last updated on 03-Mar-2019

In this tutorial, You will learn how to check if path is file or directory using nodejs.

tags nodejs

Node.js file or directory ?

require('fs') - Load the File System module

fs.statSync(path) method returns the instance of fs.Stats

stats.isFile() returns true if file path is File, otherwise returns false.

stats.isDirectory() returns true if file path is Directory, otherwise returns false.

var fs = require('fs');
var stats = fs.statSync("c:\\dog.jpg");
console.log('is file ? ' + stats.isFile());

var stats = fs.statSync("c:\\demo");
console.log('is directory ? ' + stats.isDirectory());

output

is file ? true
is directory ? true
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]