How to check if path is file or directory using nodejs

By Yashwant Chavan, Views 38268, Date 20-Feb-2017

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 Chavan

Yashwant Chavan

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@gmail.com