Open In App

Node.js fsPromises.mkdir() Method

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The fsPromises.mkdir() method is used to asynchronously create a directory then resolves the Promise with either no arguments, or the first directory path created if recursive is true.

Syntax:

fsPromises.mkdir(path, options)

Parameters: This method accept two parameters as mentioned above and described below:

  1. path: This parameter is a String, Buffer or URL and holds the path of the directory has to be created.
  2. options: It is an Object or an Integer
    • recursive: This parameter holds the recursive boolean value. By default it is false.
    • mode: The mode option is used to set the directory permission, by default it is 0777. It is a String or an Integer

Return Value: It returns the Promise object which represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Below example illustrates the use of fsPromises.mkdir() method in Node.js.

Example:




// Node.js program to demonstrate 
// the fsPromises.mkdir() Method 
    
// Include fs and path module 
const fs = require('fs');
const fsPromises = fs.promises;
  
fsPromises.mkdir('fs_test2').then(function() {
    console.log('Directory created successfully');
}).catch(function() {
    console.log('failed to create directory');
});


Output:

Directory created successfully!

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads