Skip to content

Instantly share code, notes, and snippets.

@bwinant
Created October 23, 2018 10:44
Show Gist options
  • Save bwinant/0407d4ae620f230c8252f8fbd65bb162 to your computer and use it in GitHub Desktop.
Save bwinant/0407d4ae620f230c8252f8fbd65bb162 to your computer and use it in GitHub Desktop.
mkdirs in Node.js
const fs = require('fs');
const path = require('path');
const dirToCreate;
const initDir = path.isAbsolute(dirToCreate) ? path.sep : '';
dirToCreate.split(path.sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir);
if (!fs.existsSync(curDir)) {
fs.mkdirSync(curDir);
}
return curDir;
}, initDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment