What is mkpath?
The mkpath npm package is used to create directories recursively, meaning it can create a directory and any necessary subdirectories in a single call. This is particularly useful for ensuring that a directory structure exists before writing files to it.
What are mkpath's main functionalities?
Create Nested Directories
This feature allows you to create a nested directory structure in one go. The code sample demonstrates how to create the directories 'path/to/nested/directories' and handle any potential errors.
const mkpath = require('mkpath');
mkpath('path/to/nested/directories', function (err) {
if (err) throw err;
console.log('Directories created');
});
Custom Permissions
This feature allows you to set custom permissions for the directories being created. The code sample shows how to create directories with the permission set to 0755.
const mkpath = require('mkpath');
mkpath('path/to/nested/directories', 0755, function (err) {
if (err) throw err;
console.log('Directories created with custom permissions');
});
Other packages similar to mkpath
mkdirp
The mkdirp package provides similar functionality to mkpath, allowing you to create nested directories recursively. It is widely used and has a simple API. Compared to mkpath, mkdirp is more popular and has more frequent updates.
fs-extra
The fs-extra package extends the native Node.js fs module with additional methods, including a method for creating directories recursively. It offers a broader range of file system utilities compared to mkpath, making it a more versatile choice for file system operations.
node-mkdir
The node-mkdir package is another alternative for creating directories recursively. It is lightweight and straightforward, similar to mkpath, but it is less popular and has fewer features compared to fs-extra and mkdirp.
mkpath
Make all directories in a path, like mkdir -p
.
How to use
var mkpath = require('mkpath');
mkpath('red/green/violet', function (err) {
if (err) throw err;
console.log('Directory structure red/green/violet created');
});
mkpath.sync('/tmp/blue/orange', 0700);
mkpath(path, [mode = 0777 & (~process.umask()),] [callback])
Create all directories that don't exist in path
with permissions mode
. When finished, callback(err)
fires with the error, if any.
mkpath.sync(path, [mode = 0777 & (~process.umask())]);
Synchronous version of the same. Throws error, if any.
License
This software is released under the MIT license.