How to use the mkdirsSync function from fs-extra

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

fs-extra.mkdirsSync is a function in the fs-extra library in Node.js that creates a directory and any necessary subdirectories synchronously.

119
120
121
122
123
124
125
126
127
128
const folder_name = os + pkg.devDependencies['electron'].replace(/\./g, '-');
const cache_path = path.join('./cache', folder_name);
if (fs.existsSync(cache_path)) return;

console.log(`Extracting: ${download_path}`);
fs.mkdirsSync(cache_path);
return _zipExtract(download_path, cache_path)
  .then(() => {
    const license_file = path.join('./cache', folder_name, 'LICENSE');
    if (fs.existsSync(license_file)) fs.removeSync(license_file);
fork icon169
star icon0
watch icon2

+ 9 other calls in file

215
216
217
218
219
220
221
222
223
224
    return fs.mkdirsAsync(this._pluginConfig.path)
        .then(() => this._saveDataFile(fs.writeFileAsync));
}

saveDataFileSync() {
    fs.mkdirsSync(this._pluginConfig.path);
    this._saveDataFile(fs.writeFileSync);
}

_saveDataFile(saveFn: (path: string, result: ITestResult, encode: string) => string): string {
fork icon38
star icon4
watch icon4

How does fs-extra.mkdirsSync work?

The fs-extra.mkdirsSync method is used to create a new directory or a series of nested directories in a synchronous manner, recursively creating directories if necessary, and throwing an error if the directory already exists or can't be created.

250
251
252
253
254
255
256
257
258
259
260
    if (lastSepIndex > -1) {
        outDir = outFile.substring(0, lastSepIndex);
    }


    if (!fse.existsSync(outDir)) {
        fse.mkdirsSync(outDir);
    }
}
function logSuccess(result) {
    if (result) {
fork icon0
star icon1
watch icon1

+ 5 other calls in file

171
172
173
174
175
176
177
178
179
180
    name = (name || "").replace(/([^A-Za-z0-9_-]+)/gi, "");
    const loc = path.join(this.rootLocation, name);
    if (this.cacheExists(name) && !overwrite) {
        throw `Cache "${name}" already exist! Overwriting is not allowed!`;
    }
    fs.mkdirsSync(loc);
    fs.createFileSync(path.join(loc, `${name}.json`));
    return new Cache(this.rootLocation, name);
}
getCache(name) {
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
const fs = require("fs-extra");

// Create a new directory called "myDir"
fs.mkdirsSync("./myDir");

This will create a new directory in the current working directory called "myDir". If the directory already exists, mkdirsSync will not overwrite it and will instead throw an error.

34
35
36
37
38
39
40
41
42
43
44
45
app.get('/data/:threadId', jsonParser, function (request, response) {
  folderImage = request.params["threadId"];
  console.log("folderImage: " + folderImage)
  console.log("numImage: " + numImage)


  fs.mkdirsSync('./uploads/' + folderImage, { recursive: true })


  let filesInner = fs.readdirSync('./uploads/' + folderImage);
  let jpgsInner = filesInner.filter(function(el, i) {
    return el.substring(el.length - 3) == 'jpg';
fork icon0
star icon0
watch icon1

+ 55 other calls in file

1509
1510
1511
1512
1513
1514
1515
1516
1517
const cwd = process.cwd();
let tmpDir;
beforeEach(() => {
  tmpDir = getTmpDirPath();
  serviceDir = path.join(tmpDir, 'service');
  fse.mkdirsSync(serviceDir);
  process.chdir(serviceDir);
  pluginManager.serverless.serviceDir = serviceDir;
});
fork icon0
star icon0
watch icon961

76
77
78
79
80
81
82
83
84
  confirmBlueprintedForDir('blueprints/app', 'foo-bar');
});

it('ember new @foo/bar, when direct parent directory contains `foo`', async function () {
  let scopedDirectoryPath = path.join(process.cwd(), 'foo');
  fs.mkdirsSync(scopedDirectoryPath);
  process.chdir(scopedDirectoryPath);

  await ember(['new', '@foo/bar', '--skip-npm']);
fork icon0
star icon0
watch icon159

+ 8 other calls in file

54
55
56
57
58
59
60
61
62
63
if (fes.existsSync(targetFolder)) {
  spinner.stop(); // spinner.fail(chalk.yellow('文件夹存在!'));

  resolve(true);
} else {
  fes.mkdirsSync(targetFolder);
  spinner.stop(); // spinner.succeed(chalk.yellow('文件夹创建成功!'))

  resolve(true);
}
fork icon0
star icon0
watch icon1

+ 13 other calls in file

154
155
156
157
158
159
160
161
162
163

// This is the directory we got "back" from `PackageCache.create`.
let targetDir = path.join(process.cwd(), 'tmp', 'target');
expect(dir(targetDir)).to.not.exist;
testPackageCache._conf.set('label', targetDir);
fs.mkdirsSync(targetDir);
expect(dir(targetDir)).to.exist;

// This is the directory which would be created as a link.
let eventualDir = path.join(targetDir, 'node_modules', 'beta');
fork icon0
star icon0
watch icon159

+ 5 other calls in file

439
440
441
442
443
444
445
446
447
  link = links[i];
  if (typeof link === 'string') {
    commands[type].invoke('link', link, { cwd: this.dirs[label] });
  } else {
    linkPath = path.join(this.dirs[label], translate(type, 'path'), link.name);
    fs.mkdirsSync(path.dirname(linkPath)); // Just in case the path doesn't exist.
    symlinkOrCopySync(link.path, linkPath);
  }
}
fork icon0
star icon0
watch icon159

function icon

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