How to use the mkdirSync function from graceful-fs

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

graceful-fs.mkdirSync is a method that creates a new directory with the specified name and synchronously returns undefined if successful or throws an error if it fails.

58
59
60
61
62
63
64
65
66
67
68
69
70
}


function writeFileSync(path, data, options) {
  if (!path) throw new TypeError('path is required!');


  fs.mkdirSync(dirname(path), { recursive: true });
  fs.writeFileSync(path, data, options);
}


function appendFile(path, data, options = {}) {
fork icon9
star icon48
watch icon0

+ 39 other calls in file

58
59
60
61
62
63
64
65
66
67
68
69
70


  const parent = dirname(path);
  const exist = fs.existsSync(parent);


  if (!exist) mkdirsSync(parent);
  fs.mkdirSync(path);
}


function checkParent(path) {
  if (!path) throw new TypeError('path is required!');
fork icon0
star icon1
watch icon0

+ 15 other calls in file

How does graceful-fs.mkdirSync work?

graceful-fs.mkdirSync is a method that creates a new directory with the specified name synchronously, meaning that it blocks execution until the directory is either created or the operation fails. If the directory already exists, the method does nothing and returns undefined. The method takes two arguments: the first argument is the path of the directory to be created, and the second argument is an optional object containing options that affect the behavior of the method. If the operation fails, graceful-fs.mkdirSync throws an error. The type of the error can vary depending on the specific reason for the failure, such as permission issues or an invalid path. Under the hood, graceful-fs.mkdirSync uses the fs.mkdirSync method provided by Node.js core to create the directory, but it adds additional functionality to handle errors and retry the operation in certain cases. This makes graceful-fs.mkdirSync more robust and reliable than the built-in fs.mkdirSync method in some situations, especially when working with network file systems or when multiple processes or threads are accessing the file system concurrently.

42
43
44
45
46
47
48
49
50
51

  fse.emptyDirSync(TEST_DIR)

  // Create fixtures:
  fs.writeFileSync(path.join(TEST_DIR, 'a-file'), 'sonic the hedgehog\n')
  fs.mkdirSync(path.join(TEST_DIR, 'a-folder'))
  fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-file'), 'tails\n')
  fs.mkdirSync(path.join(TEST_DIR, 'a-folder/another-folder'))
  fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
})
fork icon802
star icon0
watch icon91

+ 7 other calls in file

11
12
13
14
15
16
17
18
19
20
21
const prepareOptions = require("./helpers/prepareOptions");
const deprecationTracking = require("./helpers/deprecationTracking");
const FakeDocument = require("./helpers/FakeDocument");


function copyDiff(src, dest, initial) {
	if (!fs.existsSync(dest)) fs.mkdirSync(dest);
	const files = fs.readdirSync(src);
	files.forEach(filename => {
		const srcFile = path.join(src, filename);
		const destFile = path.join(dest, filename);
fork icon163
star icon0
watch icon24

+ 55 other calls in file

Ai Example

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

try {
  fs.mkdirSync("./my-directory");
  console.log("Directory created successfully.");
} catch (err) {
  console.error(err);
}

In this example, the fs.mkdirSync method is used to create a new directory named "my-directory" in the current working directory synchronously. The method call is wrapped in a try...catch block to handle any errors that may occur during the operation. If the directory is created successfully, the message "Directory created successfully." is logged to the console. If an error occurs, the error object is logged to the console instead.

115
116
117
118
119
120
121
122
123
124
125
  }
  return copyDir(src, dest, opts)
}


function mkDirAndCopy (srcStat, src, dest, opts) {
  fs.mkdirSync(dest, srcStat.mode)
  fs.chmodSync(dest, srcStat.mode)
  return copyDir(src, dest, opts)
}

fork icon0
star icon0
watch icon0

+ 2 other calls in file