How to use the mkdirp function from fs-extra

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

fs-extra.mkdirp is a function that creates a directory and any necessary subdirectories.

1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
let src = req.body.src;
if(!src || src.length < 15) throw "测试代码太短"
let lang = req.body.lang;

// console.log(req.files[0].path)
await fs.mkdirp(tmp_dir)
src_path = tmp_dir + "main.cpp";
await fs.writeFile(src_path, src)

input_path = tmp_dir + (problem.file_io ? problem.file_io_input_name : "test.in");
fork icon1
star icon0
watch icon1

+ 4 other calls in file

878
879
880
881
882
883
884
885
886
887
const pathsArrayPromises = _.map(
    pathsArray,
    (folderPath) => () =>
        new Promise(async (resolve, reject) => {
            try {
                await fs.mkdirp(folderPath, {
                    mode: 0o0777,
                });
            } catch (err) {
                console.error(err);
fork icon0
star icon2
watch icon3

+ 19 other calls in file

How does fs-extra.mkdirp work?

fs-extra.mkdirp is a function in the fs-extra library that creates a directory path recursively, creating all intermediate-level directories if they don't exist already. It is a wrapper around Node.js' fs.mkdir and fs.mkdirSync functions. If the directory already exists, no action is taken.

32
33
34
35
36
37
38
39
40
41
if (process.platform !== 'darwin') {
  const mksnapshotBinPath = `${baseDirPath}/node_modules/electron-mksnapshot/bin`;
  const matchingDirs = crossArchDirs.map((dir) => `${mksnapshotBinPath}/${dir}`).filter((dir) => fs.existsSync(dir));
  for (const dir of matchingDirs) {
    if (fs.existsSync(`${mksnapshotBinPath}/gen/v8/embedded.S`)) {
      await mkdirp(`${dir}/gen/v8`);
      fs.copyFileSync(`${mksnapshotBinPath}/gen/v8/embedded.S`, `${dir}/gen/v8/embedded.S`);
    }
  }
}
fork icon0
star icon0
watch icon1

+ 7 other calls in file

48
49
50
51
52
53
54
55
56
57
		},
	lockfilePath,
});

const linksDir = path.join(screenReadersPath, ".links");
await fs.mkdirp(linksDir);
await fs.writeFile(path.join(linksDir, sha1(packagePath)), packagePath);

await validateCache(packagePath, screenReadersPath, linksDir);
await releaseLock();
fork icon0
star icon0
watch icon1

+ 19 other calls in file

Ai Example

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

// Create a new directory (and its parent directories, if necessary)
fs.mkdirp("/path/to/new/directory")
  .then(() => {
    console.log("Directory created successfully!");
  })
  .catch((err) => {
    console.error("Error creating directory:", err);
  });

In this example, we're using the fs-extra library to call the mkdirp() function with the path of the directory we want to create. The function returns a Promise that resolves with no value when the directory is created successfully. If there's an error creating the directory, the Promise is rejected with an error object.

132
133
134
135
136
137
138
139
140
141
const workspaceRoot = path.resolve(__dirname, '../../../');
const outputDir = path.join(workspaceRoot, 'tmp', 'benchmarks');
const [server, browser] = await Promise.all([
  createServer({ port: PORT }),
  createBrowser(),
  fse.mkdirp(outputDir),
]);

const outputFile = fse.createWriteStream(path.join(outputDir, 'browser.log'));
// `node benchmark.js | tee outputFile`
fork icon0
star icon0
watch icon1

+ 3 other calls in file

30
31
32
33
34
35
36
37
38
    return Promise.resolve()
  }
}

async testSymlink (comboOpts, zipPath) {
  await fs.mkdirp(this.tempBase)
  const testPath = await fs.mkdtemp(path.join(this.tempBase, `symlink-test-${comboOpts.platform}-${comboOpts.arch}-`))
  const testFile = path.join(testPath, 'test')
  const testLink = path.join(testPath, 'testlink')
fork icon0
star icon0
watch icon1

+ 17 other calls in file

122
123
124
125
126
127
128
129
130
131

fs.mkdir('public/product_images/' +product._id, { recursive: true }); //thực hiện tạo folder có tên là id của sản phẩm

fs.mkdirp('public/product_images/' +product._id + '/gallery', { recursive: true }); //thêm folder gellery sau cái folder trên

fs.mkdirp('public/product_images/' +product._id + '/gallery/thumbs', { recursive: true }); //tương tự

if(imageFile != "") {
    var productImage = req.files.image;
    var path = 'public/product_images/' + product._id + '/' + imageFile; //lưu ảnh vào folder id
fork icon0
star icon0
watch icon0

+ 3 other calls in file

131
132
133
134
135
136
137
138
139
140
await npmRun('make:doc');

// Copy the version from doc/natural-lenses to the worktree
const docsSrcPath = path.resolve(__dirname, '../../doc', pkgName, version),
  docsDestPath = wt.relPath(version);
await fsx.mkdirp(docsDestPath);
await fsx.copy(docsSrcPath, docsDestPath);

// TODO: Build the index.html redirector and versions.html
const thisDocURL = `./${version}/index.html`;
fork icon0
star icon0
watch icon0

function icon

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