1 | # ensureDir(dir, [callback])
|
2 |
|
3 | Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
|
4 |
|
5 | **Aliases:** `mkdirs()`, `mkdirp()`
|
6 |
|
7 | - `dir` `<String>`
|
8 | - `callback` `<Function>`
|
9 |
|
10 | ## Example:
|
11 |
|
12 | ```js
|
13 | const fs = require('fs-extra')
|
14 |
|
15 | const dir = '/tmp/this/path/does/not/exist'
|
16 |
|
17 | // With a callback:
|
18 | fs.ensureDir(dir, err => {
|
19 | console.log(err) // => null
|
20 | // dir has now been created, including the directory it is to be placed in
|
21 | })
|
22 |
|
23 | // With Promises:
|
24 | fs.ensureDir(dir)
|
25 | .then(() => {
|
26 | console.log('success!')
|
27 | })
|
28 | .catch(err => {
|
29 | console.error(err)
|
30 | })
|
31 |
|
32 | // With async/await:
|
33 | async function example (directory) {
|
34 | try {
|
35 | await fs.ensureDir(directory)
|
36 | console.log('success!')
|
37 | } catch (err) {
|
38 | console.error(err)
|
39 | }
|
40 | }
|
41 |
|
42 | example(dir)
|
43 | ```
|