How to create full path with node's fs.mkdirSync?
Edit
NodeJS version 10.12.0
has added a native support for both mkdir
and mkdirSync
to create a directory recursively with recursive: true
option as the following:
fs.mkdirSync(targetDir, { recursive: true });
And if you prefer fs Promises API
, you can write
fs.promises.mkdir(targetDir, { recursive: true });
Original Answer
Create directories recursively if they do not exist! (Zero dependencies)
const fs = require('fs');
const path = require('path');
function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : '';
const baseDir = isRelativeToScript ? __dirname : '.';
return targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code === 'EEXIST') { // curDir already exists!
return curDir;
}
// To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
if (err.code === 'ENOENT') { // Throw the original parentDir error on curDir `ENOENT` failure.
throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`);
}
const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1;
if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) {
throw err; // Throw if it's just the last created dir.
}
}
return curDir;
}, initDir);
}
Usage
// Default, make directories relative to current working directory.
mkDirByPathSync('path/to/dir');
// Make directories relative to the current script.
mkDirByPathSync('path/to/dir', {isRelativeToScript: true});
// Make directories with an absolute path.
mkDirByPathSync('/path/to/dir');
Demo
Try It!
Explanations
- [UPDATE] This solution handles platform-specific errors like
EISDIR
for Mac andEPERM
andEACCES
for Windows. Thanks to all the reporting comments by @PediT., @JohnQ, @deed02392, @robyoder and @Almenon. - This solution handles both relative and absolute paths. Thanks to @john comment.
- In the case of relative paths, target directories will be created (resolved) in the current working directory. To Resolve them relative to the current script dir, pass
{isRelativeToScript: true}
. - Using
path.sep
andpath.resolve()
, not just/
concatenation, to avoid cross-platform issues. - Using
fs.mkdirSync
and handling the error withtry/catch
if thrown to handle race conditions: another process may add the file between the calls tofs.existsSync()
andfs.mkdirSync()
and causes an exception.- The other way to achieve that could be checking if a file exists then creating it, I.e,
if (!fs.existsSync(curDir) fs.mkdirSync(curDir);
. But this is an anti-pattern that leaves the code vulnerable to race conditions. Thanks to @GershomMaes comment about the directory existence check.
- The other way to achieve that could be checking if a file exists then creating it, I.e,
- Requires Node v6 and newer to support destructuring. (If you have problems implementing this solution with old Node versions, just leave me a comment)
A more robust answer is to use use mkdirp.
var mkdirp = require('mkdirp');
mkdirp('/path/to/dir', function (err) {
if (err) console.error(err)
else console.log('dir created')
});
Then proceed to write the file into the full path with:
fs.writeFile ('/path/to/dir/file.dat'....
One option is to use shelljs module
npm install shelljs
var shell = require('shelljs');
shell.mkdir('-p', fullPath);
From that page:
Available options:
p: full path (will create intermediate dirs if necessary)
As others have noted, there's other more focused modules. But, outside of mkdirp, it has tons of other useful shell operations (like which, grep etc...) and it works on windows and *nix