DEV Community

Paramanantham Harrison for JS Mates

Posted on • Originally published at jsmates.com on

Check whether folder exists on the path using Node Js

fs module in Node Js has methods to check whether a folder exists in the path. We will use fs.existsSync to check whether the folder exists or not.

const fs = require('fs');

// Check folder
const checkFolder = folderPath => {
  // Throw error if folder path doesn't exist
  if (!folderPath) throw Error('folder path is required');

  // Check folder exists in the path using `fs.existsSync`
  const isFolderExist = fs.existsSync(folderPath);
  return isFolderExist;
};

console.log(checkFolder('blog')); // True
console.log(checkFolder('sunrises')); // False
Enter fullscreen mode Exit fullscreen mode

This way, you can check whether a folder exists or not in Node Js.

Top comments (2)

Collapse
 
martin2844 profile image
martin2844

Nice one, easy to follow

Collapse
 
sanyaldips93 profile image
Dipayan Sanyal

Cool one. I am going to try this out.