UNPKG

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