How to use the mkdirs function from fs-extra

Find comprehensive JavaScript fs-extra.mkdirs code examples handpicked from public code repositorys.

fs-extra.mkdirs is a method in the fs-extra library that creates a directory and any necessary subdirectories recursively in a file system.

135
136
137
138
139
140
141
142
143
144
 * @param {object|string} options - see fs.writeFile, `flag` must not be set.
 */
async function safeOutputFile(file, data, options={}) {
	let directory = path.dirname(file);
	if (!await fs.pathExists(directory)) {
		await fs.mkdirs(directory);
	}
	let temporary = `${file}.tmp`;
	let fd = await fs.open(temporary, "w");
	try {
fork icon58
star icon228
watch icon0

849
850
851
852
853
854
855
856
857
858
859


async function createPrintedFolder({
	files,
}) {
	const destDir = path.join(PRINTED_DIR, getCurMoment());
	await fs.mkdirs(
		destDir,
	);
	await fs.writeJSON(
		path.join(destDir, PRINT_JSON),
fork icon0
star icon2
watch icon1

+ 19 other calls in file

How does fs-extra.mkdirs work?

fs-extra.mkdirs is a method in the fs-extra library that creates a directory and any necessary subdirectories recursively in a file system. When you call fs-extra.mkdirs(path), the function creates the directory specified by path and any necessary parent directories recursively, if they do not already exist. If the directory already exists, mkdirs does not throw an error. For example, if you call fs-extra.mkdirs('/foo/bar/baz'), and the foo and bar directories do not exist, mkdirs will create them before creating the baz directory. fs-extra.mkdirs returns a Promise that resolves with the path to the created directory, or rejects with an error if the directory could not be created. In essence, fs-extra.mkdirs provides a way to create a directory and any necessary parent directories recursively, making it easy to ensure that a file system path exists before trying to write or read files from it.

10
11
12
13
14
15
16
17
18
19
20
21
}


async function createDirectory({
  path: directoryPath,
}) {
  const createdPath = await fs.mkdirs(directoryPath);


  return createdPath === undefined ? "Directory Already Exists" : "Directory Created";
}

fork icon0
star icon0
watch icon12

+ 5 other calls in file

16
17
18
19
20
21
22
23
24
25
26
27
var fs = require('fs-extra');
var when = require('when');
var nodeFn = require('when/node/function');
var keys = require('when/keys');
var fspath = require("path");
var mkdirp = fs.mkdirs;


var log = require("../log");


var promiseDir = nodeFn.lift(mkdirp);
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const fse = require("fs-extra");

const path = "/foo/bar/baz";

fse
  .mkdirs(path)
  .then(() => {
    console.log(`Created directory: ${path}`);
  })
  .catch((err) => {
    console.error(`Error creating directory: ${path}`);
    console.error(err);
  });

In this example, we first import the fs-extra library. We then specify the directory we want to create: /foo/bar/baz. Next, we call fse.mkdirs(path) to create the directory and any necessary parent directories recursively. If the directory is successfully created, the then block will execute and log a message to the console. If there is an error creating the directory, the catch block will execute and log an error message and the error object to the console. Note that fs-extra provides other methods for manipulating the file system, such as mkdirp, ensureDir, ensureFile, and ensureLink, which provide similar functionality to mkdirs for other types of file system objects.

87
88
89
90
91
92
93
94
95
96
97
98
99
  })
}




function ensureDir(src, callback) { return fs.ensureDir(src, _.cb(callback)) }
function mkdirs(src, callback) { return fs.mkdirs(src, _.cb(callback)) }
function emptyDir(src, callback) { return fs.emptyDir(src, _.cb(callback)) }


function readFile(src, callback) { return fs.readFile(src, 'utf8', _.cb(callback)) }
function outputFile(src, data, callback) {
fork icon0
star icon0
watch icon0

+ 9 other calls in file

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)