THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This article covers multiple ways to create a directory Nodejs. One way using fs.mkdir
using asynchronous callback, Another way using mkdirSync
Sometimes, We need to check if a folder exists or not. Nodejs provides an inbuilt fs module that provides multiple functions.
This is an asynchronous version of the checking folder that exists or not.
Syntax.
fs.mkdirSync(path,[options])
path
: the path of the folder to create
options: recursive
to true
or false
.
const fs = require("fs");
if(fs.existsSync("folder") || fs.mkdirSync("folder")){
console.log("Folder exists")
}
The above code does not work if the path contains folder1/folder2.
For this we need provide below code
const fs = require("fs");
if(fs.existsSync("folder") || fs.mkdirSync("folder",{recursive:true})){
console.log("Folder exists")
}
Similarly, It provides mkdir
() function which is aschronous version of an mkdirSync
.
const directorypath="folder";
fs.access(directorypath, (error) => {
fs.mkdir(directorypath, { recursive: true }, (err,data) => {
if (err) throw err;
console.log("folder created",true)
});
🧮 Tags
Recent posts
Multiple ways to iterate a loop with index and element in array in swift How to reload a page/component in Angular? How to populate enum object data in a dropdown in angular| Angular material dropdown example How to get the current date and time in local and UTC in Rust example Angular 13 UpperCase pipe tutorial | How to Convert a String to Uppercase exampleRelated posts