Very simple and really awesome solution that I found on stackoverflow

In ES2015

import mkdirp from 'mkdirp'  
import fs from 'fs'  
import { dirname } from 'path'  
function writeFile (path, contents, cb) {  
  mkdirp(dirname(path), (err) => {
    if (err) return cb(err)
    fs.writeFile(path, contents, cb)
  })
}

In ES5

var mkdirp = require("mkdirp")  
var fs = require("fs")  
var getDirName = require("path").dirname  
function writeFile (path, contents, cb) {  
  mkdirp(getDirName(path), function (err) {
    if (err) return cb(err)
    fs.writeFile(path, contents, cb)
  })
}

© 2016. All Rights Reserved.

Proudly published with Ghost