How to create a directory using Node.js ? - GeeksforGeeks

07 Oct, 2021
In this article, we will create a directory using NodeJS.
NodeJS has Filesystem(fs) core module, which enables interacting with the file system, has Node.js fs.mkdir() method or Node.js fs.mkdirSync() method method, to create new directory /parent directory.
Node.js fs.mkdir() method: Let’s create a new directory using fs.mkdir() method. Initially, we have single file index.js, as we can see in the given image.
index.js
Example: Edit the index.js file.
Javascript
const fs = require("fs");  const path = "./new-Directory";  fs.access(path, (error) => {     // To check if the given directory   // already exists or not  if (error) {    // If current directory does not exist    // then create it    fs.mkdir(path, (error) => {      if (error) {        console.log(error);      } else {        console.log("New Directory created successfully !!");      }    });  } else {    console.log("Given Directory already exists !!");  }});
Output:
You can check the terminal output.
After executing the above code, node.js will create a new directory if it does not exist. A new Directory named — “new-Directory” is created.
Creating Parent Directories: If we want to create multilevel directory, fs.mkdir() has optional recursive Boolean value we can pass as a parameter. 
Javascript
const fs = require("fs");  // Multilevel directoryconst path = "./directory1/directory2/new-directory";  fs.access(path, (error) => {    // To check if given directory   // already exists or not  if (error) {    // If current directory does not exist then create it    fs.mkdir(path, { recursive: true }, (error) => {      if (error) {        console.log(error);      } else {        console.log("New Directory created successfully !!");      }    });  } else {    console.log("Given Directory already exists !!");  }});
Output:
New Directory created successfully.index.js
index.js
We can see that a multilevel directory “directory1\directory2\new-directory” is created.index.js
We can see that a multilevel directory “directory1\directory2\new-directory” is created.
index.js
Removing a folder: If we want to delete a given directory, we can use Node.js fs.rmdir() Method or Node.js fs.rmdirSync() Method, it will become complicated if the directory contain some file content.
So we can use a third party package fs-extra provided by npm to delete the given directory. Let’s install the given package using npm.
Run the following command in your command line
npm install fs-extra
Example: Now run the following code to delete the given directory
Javascript
const fs1 = require("fs-extra");  const path = "./directory1";  fs1.remove(path, (error) => {  if (error) {    console.log(error);  } else {    console.log("Folder Deleted Successfully !!");  }});
Output
index.js
Node.js fs.mkdirSync() method: Let’s create a new directory using fs.mkdirSync() method. Initially, we have single file index.js, as we can see in the given image.
Example:
Javascript
const fs1 = require("fs-extra");  // Node.js program to demonstrate the// fs.mkdirSync() method  const fs = require("fs");  const path = require("path");  // Using fs.exists() method to// Check that the directory exists or notconsole.log("Checking for directory" + path.join(__dirname, "Tisu"));fs.exists(path.join(__dirname, "Tisu"), (exists) => {  console.log(exists ? "The directory already exists" : "Not found!");});  // Using fs.mkdirSync() method// To create the directory recursivelyfs.mkdirSync(path.join(__dirname, "Tisu"), true);  // Using fs.exists() method to// Check that the directory exists or notfs.exists(path.join(__dirname, "Tisu"), (exists) => {  console.log(exists ? "The directory already exists" : "Not found!");});
Output:
NodeJS-Questions
Picked
Technical Scripter 2020
Node.js
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFileSync() Method
How to update Node.js and NPM to next version ?
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Difference between Fetch and Axios.js for making http requests
How to read and write Excel file in Node.js ?
Express.js res.render() Function
Express.js res.redirect() Function