ensureDir(dir, [callback])

Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p.

Aliases: mkdirs(), mkdirp()

  • dir <String>
  • callback <Function>

Example:

  1. const fs = require('fs-extra')
  2. const dir = '/tmp/this/path/does/not/exist'
  3. fs.ensureDir(dir, err => {
  4. console.log(err) // => null
  5. // dir has now been created, including the directory it is to be placed in
  6. })
  7. // With Promises:
  8. fs.ensureDir(dir)
  9. .then(() => {
  10. console.log('success!')
  11. })
  12. .catch(err => {
  13. console.error(err)
  14. })