How to use the mkdir function from graceful-fs

Find comprehensive JavaScript graceful-fs.mkdir code examples handpicked from public code repositorys.

The graceful-fs.mkdir function creates a directory, similar to the built-in fs.mkdir, but with added error handling and file system caching.

139
140
141
142
143
144
145
146
147
148
    copyDir(dir.name)
  })
}

function mkDir (dir, target) {
  fs.mkdir(target, dir.mode, function (err) {
    if (err) return onError(err)
    // despite setting mode in fs.mkdir, doesn't seem to work
    // so we set it here.
    fs.chmod(target, dir.mode, function (err) {
fork icon1
star icon0
watch icon0

73
74
75
76
77
78
79
80
81
82

function createBuildDir () {
  log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)

  const deepestBuildDirSubdirectory = win ? buildDir : buildBinsDir
  fs.mkdir(deepestBuildDirSubdirectory, { recursive: true }, function (err, isNew) {
    if (err) {
      return callback(err)
    }
    log.verbose(
fork icon0
star icon0
watch icon1

How does graceful-fs.mkdir work?

graceful-fs.mkdir is a method that creates a new directory with the specified path, ensuring that parent directories exist as well and providing a graceful fallback when there are errors. It is a drop-in replacement for the built-in fs.mkdir method in Node.js. When a directory is created, its file mode is set to 0o777 (all permissions).

13
14
15
16
17
18
19
20
21
22
}

var mode = customMode || (DEFAULT_DIR_MODE & ~process.umask());
dirpath = path.resolve(dirpath);

fs.mkdir(dirpath, mode, onMkdir);

function onMkdir(mkdirErr) {
  if (!mkdirErr) {
    return fs.stat(dirpath, onStat);
fork icon0
star icon0
watch icon1

142
143
144
145
146
147
148
149
150
151
fs.stat(directory, function (err, stat) {
  if (stat && stat.isDirectory()) {
    done()
  } else {
    mkdir(path.dirname(directory), function () {
      fs.mkdir(directory, done)
    })
  }
})
/* eslint-enable handle-callback-err */
fork icon0
star icon0
watch icon1

+ 13 other calls in file

Ai Example

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

fs.mkdir("/path/to/new/directory", function (err) {
  if (err) {
    console.error(err);
  } else {
    console.log("Directory created successfully!");
  }
});

In this example, fs.mkdir is used to create a new directory at the path /path/to/new/directory. The function passed as the second argument is a callback that will be executed once the operation is complete. If an error occurs, it will be passed to the callback as the first argument, and if the directory is created successfully, the callback will be called without any arguments.

70
71
72
73
74
75
76
77
78
79
  }
}

function createBuildDir () {
  log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
  fs.mkdir(buildDir, { recursive: true }, function (err, isNew) {
    if (err) {
      return callback(err)
    }
    log.verbose(
fork icon0
star icon0
watch icon1

+ 9 other calls in file