How to check if path is file or directory using nodejs

 Posted On  | Yashwant Chavan 

In this tutorial, You will learn how to check if path is file or directory using 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


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0342 seconds.