Node.js fs-extra ensureDirSync() function

06 Oct, 2021
The ensureDirSync() function is the synchronous version of ensureDir() function. The function makes sure that the directory exists, if the directory structure does not exist it will be created by the function. mkdirsSync() and mkdirpSync() can also be used in place of ensureDirSync() and the result will be same.
Syntax:
ensureDirSync(dir,options)
// OR
mkdirsSync(dir,options)
// OR
mkdirpSync(dir,options)
Parameters:
dir: It is a string that contains the directory path.
options: It is an object or an integer that is used to specify the optional parameters.Integer: If it is an integer it will be mode.Object: If it is an object it will be {mode: integer}.
Integer: If it is an integer it will be mode.Object: If it is an object it will be {mode: integer}.
Integer: If it is an integer it will be mode.
Object: If it is an object it will be {mode: integer}.
Return value: It does not return anything.
Follow the steps to implement the function:
The module can be installed by using the following command.npm install fs-extraAfter the installation of the module you can check the version of the installed module by using this command.npm ls fs-extraCreate a file with the name index.js and require the fs-extra module in the file using the following command.const fs = require('fs-extra');To run the file write the following command in the terminal.node index.js
The module can be installed by using the following command.npm install fs-extra
npm install fs-extra
After the installation of the module you can check the version of the installed module by using this command.npm ls fs-extra
npm ls fs-extra
Create a file with the name index.js and require the fs-extra module in the file using the following command.const fs = require('fs-extra');
const fs = require('fs-extra');
To run the file write the following command in the terminal.node index.js
node index.js
Project Structure: The project structure will look like this.
Example 1:
index.js
// Requiring moduleconst fs = require("fs-extra");  // Function to check// if directory  exists// or notconst dirExists = (dir) => {  if (fs.existsSync(dir)) {    return "Directory exists";  } else {    return "Directory do not exist";  }};  // This directory// already exists so// function will not// do anythingconst dir = "dir";  // Checking before// calling functionconst before = dirExists(dir);console.log(`Before function call ${before}`);  // Function callfs.ensureDirSync(dir);  // Checking after//  calling functionconst after = dirExists(dir);console.log(`After function call ${after}`);
Output:
Example 2:
index.js
// Requiring moduleconst fs = require("fs-extra");  // Function to check// if directory  exists// or notconst dirExists = (dir) => {  if (fs.existsSync(dir)) {    return "Directory exists";  } else {    return "Directory do not exist";  }};  // This directory// do not  exists so// function will create// the directoryconst dir = "direc/dir";  const mode = 0o2775;  // Checking before// calling functionconst before = dirExists(dir);console.log(`Before function call ${before}`);  // Function callfs.ensureDirSync(dir, mode);  // Checking after//  calling functionconst after = dirExists(dir);console.log(`After function call ${after}`);
Output:
Reference: https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/ensureDir-sync.md
NodeJS-fs-extra
NodeJS-function
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.