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
This way, you can check whether a folder exists or not in Node Js.
Top comments (2)
Nice one, easy to follow
Cool one. I am going to try this out.