Today's npm package: is-directory

Subscribe to my newsletter and never miss my upcoming articles

Today's npm package is is-directory

This package will help you check if the supplied path is a directory or not. It will return true if the file path exists on the file system and its directory.

const fs = require('fs');
const isDirectory = require('is-directory');

isDirectory('dist', function(err, dir) {
  if (err) { // directory doesn't exist or isn't a directory
    fs.mkdirSync('dist') // make the directory
  }

  console.log(dir);
  //=> true
});

Or synchronized version as:

const fs = require('fs');
const isDirectory = require('is-directory');

if (!isDirectory.sync('dist')) { // if directory doesn't exist
  fs.mkdirSync('dist') // make the directory
}

Comments (2)

Catalin Pit's photo

Can't we do that with the fs module in Node.js already? 🤔

Pankaj Patel's photo

Yes we can; and apparently the package is using fs under the hood

github.com/jonschlinkert/is-directory/blob/..