write file to folder nodejs code example
Example 1: javascript fs write file with folder
var path = require('path'),
fs = require('fs');
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
Example 2: create directory when writing to file in nodejs
fs.mkdir now accepts a { recursive: true } option like so:
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});