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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay