What is mkdirp?
The mkdirp npm package is a utility module that allows you to create directories and all necessary subdirectories in a single command, similar to the 'mkdir -p' command in UNIX systems. It is useful for ensuring that a given directory path exists without manually checking and creating each level of the directory structure.
What are mkdirp's main functionalities?
Create a directory with subdirectories
This feature allows you to create a directory and any necessary parent directories. The function returns a promise that resolves to the first directory that had to be created, or null if all directories already existed.
const mkdirp = require('mkdirp');
mkdirp('/tmp/some/long/path').then(made => console.log(`Made directories: ${made}`));
Use with async/await
This feature demonstrates how mkdirp can be used with async/await syntax for cleaner, more readable asynchronous code.
const mkdirp = require('mkdirp');
(async () => {
try {
const made = await mkdirp('/tmp/some/long/path');
console.log(`Made directories: ${made}`);
} catch (e) {
console.error(e);
}
})();
Custom file mode (permissions)
This feature allows you to specify the file mode (permissions) when creating directories. The mode can be set so that the created directories have the desired permissions.
const mkdirp = require('mkdirp');
mkdirp('/tmp/some/long/path', { mode: 0o775 }).then(made => console.log(`Made directories with mode: ${made}`));
Other packages similar to mkdirp
fs-extra
fs-extra is a package that builds on the native fs module, providing additional methods and ensuring consistency across different platforms. It includes a method called 'ensureDir' which is similar to mkdirp's functionality, ensuring that a directory exists and creating it if necessary.
make-dir
make-dir is another package that offers similar functionality to mkdirp. It provides a method to create a directory and its parents if needed. One of the differences is that make-dir uses the native fs.mkdir/mkdirSync with the recursive option when available (Node.js v10.12.0 or later), potentially providing better performance on newer Node.js versions.
mkdirp
Like mkdir -p
, but in node.js!
example
pow.js
var mkdirp = require('mkdirp');
mkdirp('/tmp/foo/bar/baz', function (err) {
if (err) console.error(err)
else console.log('pow!')
});
Output
pow!
And now /tmp/foo/bar/baz exists, huzzah!
methods
var mkdirp = require('mkdirp');
mkdirp(dir, opts, cb)
Create a new directory and any necessary subdirectories at dir
with octal
permission string opts.mode
. If opts
is a non-object, it will be treated as
the opts.mode
.
If opts.mode
isn't specified, it defaults to 0777 & (~process.umask())
.
cb(err, made)
fires with the error or the first directory made
that had to be created, if any.
You can optionally pass in an alternate fs
implementation by passing in
opts.fs
. Your implementation should have opts.fs.mkdir(path, mode, cb)
and
opts.fs.stat(path, cb)
.
mkdirp.sync(dir, opts)
Synchronously create a new directory and any necessary subdirectories at dir
with octal permission string opts.mode
. If opts
is a non-object, it will be
treated as the opts.mode
.
If opts.mode
isn't specified, it defaults to 0777 & (~process.umask())
.
Returns the first directory that had to be created, if any.
You can optionally pass in an alternate fs
implementation by passing in
opts.fs
. Your implementation should have opts.fs.mkdirSync(path, mode)
and
opts.fs.statSync(path)
.
usage
This package also ships with a mkdirp
command.
usage: mkdirp [DIR1,DIR2..] {OPTIONS}
Create each supplied directory including any necessary parent directories that
don't yet exist.
If the directory already exists, do nothing.
OPTIONS are:
-m, --mode If a directory needs to be created, set the mode as an octal
permission string.
install
With npm do:
npm install mkdirp
to get the library, or
npm install -g mkdirp
to get the command.
license
MIT