How to use the mkdirSync function from fs-extra

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

fs-extra.mkdirSync is a function in the fs-extra library that creates a new directory synchronously.

38
39
40
41
42
43
44
45
46
47
48
});


gulp.task('createNycFolder', async (done) => {
    try {
        const fs = require('fs');
        fs.mkdirSync(path.join(__dirname, '.nyc_output'));
    } catch (e) {
        //
    }
    done();
fork icon209
star icon955
watch icon43

+ 9 other calls in file

27
28
29
30
31
32
33
34
35
36
37
 *
 * @returns {string}
 */
function mkdtempSync () {
  const tempdir = _mkdtempPath()
  fs.mkdirSync(tempdir, { recursive: true })
  return tempdir
}


/**
fork icon234
star icon281
watch icon77

+ 2 other calls in file

How does fs-extra.mkdirSync work?

fs-extra.mkdirSync is a function that creates a new directory synchronously. It takes two arguments: the path of the directory to create, and an optional options object. If the directory already exists, fs-extra.mkdirSync will throw an error.

The options object can include any of the following properties:

  • mode: the permissions to set on the new directory (default is 0o777)
  • recursive: if true, fs-extra.mkdirSync will create all parent directories that don't already exist (default is false)

When called, fs-extra.mkdirSync will create the new directory at the specified path, with the specified permissions if provided. If the recursive option is true, fs-extra.mkdirSync will create any necessary parent directories as well. If the directory already exists, fs-extra.mkdirSync will throw an error.

Because fs-extra.mkdirSync is a synchronous function, it will block the execution of your program until the directory has been created. If you need to create directories asynchronously, you can use the fs-extra function mkdir instead, which returns a promise.

135
136
137
138
139
140
141
142
143
```
const folderName = '/Users/flavio/test'
```

```
try {  if (!fs.existsSync(dir)){    fs.mkdirSync(dir)  }} catch (err) {  console.error(err)}
```

#### Read the content of a directory
fork icon191
star icon1
watch icon0

+ 13 other calls in file

204
205
206
207
208
209
210
211
212
213
214
    // Delete the dist folder
    if (fsExtra.existsSync(_distFolder)) {
      log(chalk.yellow(`Deleting dist folder: ${_distFolder}`));
      fsExtra.rmdirSync(_distFolder, { recursive: true });
    }
    fsExtra.mkdirSync(_distFolder);
  }
}


/**
fork icon114
star icon127
watch icon0

Ai Example

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

try {
  fs.mkdirSync("/path/to/new/directory");
  console.log("Directory created successfully");
} catch (err) {
  console.error("Error creating directory:", err);
}

In this example, we call fs-extra.mkdirSync with the path of the new directory we want to create. If the directory doesn't already exist, fs-extra.mkdirSync will create it. If an error occurs while creating the directory, fs-extra.mkdirSync will throw an error, which we catch using a try...catch block. If no errors occur, we log a success message to the console. Note that fs-extra.mkdirSync is a synchronous function, which means that it will block the execution of your program until it has finished creating the directory. If you need to create directories asynchronously, you can use the fs-extra function mkdir instead, which returns a promise.

51
52
53
54
55
56
57
58
59
60
    leDir = `${configPath}/${leSettings.path || 'letsencrypt'}`;
}

try {
    if (!fs.existsSync(leDir)) {
        fs.mkdirSync(leDir);
    }
} catch (err) {
    log.error(
        `Lets encrypt Directory does not exist and can not be created as ${leDir}: ${err.message}. We reuse old certificates`
fork icon54
star icon272
watch icon23

+ 7 other calls in file

1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
// Build the full destination path
const destinationFolderPath = fsPath.normalize(process.cwd() +
          '/_site/epub/' + destinationFolder)

// Create the destination directory
fs.mkdirSync(destinationFolderPath, { recursive: true })

// Track how many files we have to copy
const totalFiles = arrayOfPaths.length
let totalCopied = 0
fork icon39
star icon105
watch icon0

74
75
76
77
78
79
80
81
82
83
    throw new Error('Output path not specified');
}

// create paths if not existing
if (!fse.existsSync(cache)) {
    fse.mkdirSync(cache);
};

if (!fse.existsSync(output)) {
    fse.mkdirSync(output)
fork icon15
star icon185
watch icon7

+ 55 other calls in file

89
90
91
92
93
94
95
96
97
// clear temporary files
fse.emptyDirSync(cache);
console.log(enhancrPrefix + " tmp directory cleared");

if (!fse.existsSync(previewPath)) {
    fse.mkdirSync(previewPath);
};

terminal.innerHTML += '\r\n' + enhancrPrefix + ' Preparing media for restoration process..';
fork icon15
star icon185
watch icon7

+ 55 other calls in file

121
122
123
124
125
126
127
128
129
130

const folderName = '/Users/flavio/test';

try {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir);
    }
} catch (err) {
    console.error(err);
}
fork icon14
star icon101
watch icon3

+ 11 other calls in file

67
68
69
70
71
72
73
74
75
76
77
78
79
});


new Promise(async res => {
    await clearLogs()


    if(!fs.existsSync(`./etc/`)) fs.mkdirSync(`./etc/`);
    fs.appendFileSync(`./etc/debug.log`, `\n\n-- STARTED AT ${(moment().format(`MMM DD [at] hh:mm:ss A`)).toUpperCase()} --\n`);


    let ctx = {};

fork icon1
star icon4
watch icon1

+ 5 other calls in file

205
206
207
208
209
210
211
212
213
214
);

let permalink = frontmatter.permalink.slice(1);
if (permalink.length === 0) permalink = 'index';
const dest = path.resolve('./src/build/', permalink);
fs.mkdirSync(path.parse(dest).dir, { recursive: true });

if (!frontmatter.head) {
  frontmatter.head = {};
}
fork icon11
star icon33
watch icon0

+ 9 other calls in file

62
63
64
65
66
67
68
69
70
71
if(!fs.existsSync(webDir)){
    fs.mkdirSync(webDir);
}

if(!fs.existsSync(overlayDir)){
    fs.mkdirSync(overlayDir);
}

if(!fs.existsSync(utilityDir)){
    fs.mkdirSync(utilityDir);
fork icon3
star icon30
watch icon0

+ 43 other calls in file

34
35
36
37
38
39
40
41
42
43
const toT = getMTime(to)
const fromT = getMTime(from)
if (toT === undefined || fromT > toT + 1) {
    console.debug(`cp ${from} ${to}`)
    try {
        fs.mkdirSync(path.dirname(to))
    } catch {}
    fs.copyFileSync(from, to)
    fs.utimesSync(to, new Date(), new Date(fromT))
}
fork icon4
star icon24
watch icon9

+ 5 other calls in file

5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
});

await Jimp.read(original_image_path)
  .then(async (file) => {
    if (!fs.existsSync(destination_image_path)) {
      fs.mkdirSync(destination_image_path, { recursive: true });
    }

    try {
      if (fs.existsSync(converted_image_file)) {
fork icon9
star icon22
watch icon9

+ 29 other calls in file

5
6
7
8
9
10
11
12
13
14
description: string('cli.config.description'),
aliases: ['cfg'],
async execute(args) {
    const info = require("../package.json");
    if (info.build === undefined) return console.log(string('cli.not_installer'));
    if (!fs.existsSync('./config')) fs.mkdirSync('./config');
    if (!args[0]) {
        return console.log(string('cli.config.miss_key'));
    }
    const file = fs.readdirSync('./cli/config/').filter(file => file.endsWith('.js'));
fork icon6
star icon7
watch icon1

+ 8 other calls in file

102
103
104
105
106
107
108
109
110
111

const folderName = '/Users/joe/test'

try {
  if (!fs.existsSync(folderName)) {
    fs.mkdirSync(folderName)
  }
} catch (err) {
  console.error(err)
}
fork icon0
star icon15
watch icon1

+ 13 other calls in file

402
403
404
405
406
407
408
409
410
411
412
// 创建所有目录
exports.mkdirs = function (dirpath) {
  let self = this
  if (!fs.existsSync(dirpath)) {
    self.mkdirs(path.dirname(dirpath))
    fs.mkdirSync(dirpath)
  }
}


function transformByDPR (a, width, dpr) {
fork icon0
star icon2
watch icon0

92
93
94
95
96
97
98
99
100
101

const folderName = '/Users/joe/test';

try {
    if (!fs.existsSync(folderName)) {
        fs.mkdirSync(folderName);
    }
} catch (err) {
    console.error(err);
}
fork icon0
star icon1
watch icon1

+ 13 other calls in file

252
253
254
255
256
257
258
259
260
261
262
  let workdir;
  const tmpBaseDir = os.tmpdir();
  while (!workdir || fs.existsSync(workdir)) {
    workdir = path.join(tmpBaseDir, crypto.randomBytes(20).toString('hex'));
  }
  fs.mkdirSync(workdir);
  return workdir;
};


describe('convert', () => {
fork icon168
star icon237
watch icon0

36
37
38
39
40
41
42
43
44
45
  log.transports.console.level = false;
}
(async () => {
  try {
    if (!fs.existsSync(miroWorkspaceDir)) {
      fs.mkdirSync(miroWorkspaceDir);
    }
  } catch (e) {
    log.error('Could not create miro workspace!');
  }
fork icon1
star icon7
watch icon4

+ 9 other calls in file

function icon

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