DEV Community

JS Mates

Check whether folder exists on the path using Node Js

Paramanantham Harrison
Originally published at on ・1 min read

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.

Discussion (2)

martin2844 profile image

Nice one, easy to follow

sanyaldips93 profile image
Dipayan Sanyal

Cool one. I am going to try this out.