make-dir vs mkdirp vs mkdirp-promise vs fs-extra
Node.js File System Utilities
make-dirmkdirpmkdirp-promisefs-extraSimilar Packages:

Node.js File System Utilities

These packages provide various utilities for file system operations in Node.js, enhancing the built-in 'fs' module with additional functionalities. They simplify tasks such as creating directories, ensuring that directories exist, and providing promises for asynchronous operations. This makes file handling more efficient and user-friendly, especially in modern JavaScript development where promises and async/await patterns are prevalent.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
make-dir109,775,4644789.56 kB07 months agoMIT
mkdirp109,137,302197107 kB13 years agoMIT
mkdirp-promise701,18030-29 years agoISC
fs-extra09,61757.7 kB12a month agoMIT

Feature Comparison: make-dir vs mkdirp vs mkdirp-promise vs fs-extra

Functionality

  • make-dir:

    make-dir focuses solely on creating directories, ensuring that all necessary parent directories are created as well. It is lightweight and straightforward, making it easy to use for simple directory creation tasks.

  • mkdirp:

    mkdirp is specifically designed for creating nested directories. It provides a simple API for ensuring that all parent directories are created, which is particularly useful for building directory structures in a straightforward manner.

  • mkdirp-promise:

    mkdirp-promise extends mkdirp by providing a promise-based API, allowing for cleaner asynchronous code. It is ideal for modern JavaScript applications that utilize async/await.

  • fs-extra:

    fs-extra offers a wide range of file system methods, including copy, move, and remove, in addition to standard fs functionalities. It is designed to handle complex file operations seamlessly, making it suitable for various file manipulation tasks.

API Style

  • make-dir:

    make-dir has a minimalistic API focused on directory creation. It offers both synchronous and asynchronous methods, making it versatile for different coding styles, though it is primarily used in asynchronous contexts.

  • mkdirp:

    mkdirp offers a callback-based API, which may be more familiar to developers accustomed to traditional Node.js patterns. This can be beneficial for those who prefer callbacks over promises.

  • mkdirp-promise:

    mkdirp-promise provides a promise-based API, aligning with modern JavaScript practices. It allows for cleaner code using async/await, making it easier to manage asynchronous operations.

  • fs-extra:

    fs-extra provides a rich API that combines both synchronous and asynchronous methods, allowing developers to choose the style that best fits their needs. Its methods are designed to be intuitive and easy to use, catering to a wide range of file operations.

Error Handling

  • make-dir:

    make-dir handles errors gracefully, ensuring that if a directory already exists, it does not throw an error, which simplifies the logic for directory creation.

  • mkdirp:

    mkdirp's error handling is straightforward, but it may require additional checks for existing directories, as it does not inherently prevent errors when trying to create an already existing directory.

  • mkdirp-promise:

    mkdirp-promise also handles errors well, providing promise rejections that can be caught and managed using standard promise error handling techniques.

  • fs-extra:

    fs-extra includes built-in error handling for its methods, providing informative error messages that help developers troubleshoot issues related to file operations effectively.

Performance

  • make-dir:

    make-dir is lightweight and performs efficiently for directory creation tasks, ensuring that the process is quick even when creating multiple levels of directories.

  • mkdirp:

    mkdirp performs well for creating nested directories, but its performance may vary depending on the depth of the directory structure being created.

  • mkdirp-promise:

    mkdirp-promise maintains good performance while providing promise support, allowing for non-blocking directory creation without significant overhead.

  • fs-extra:

    fs-extra is optimized for performance, especially when dealing with large file operations, as it uses efficient algorithms for copying and moving files, minimizing overhead.

Community and Maintenance

  • make-dir:

    make-dir is a simpler package with a smaller scope, but it is also actively maintained and has a growing user base, making it a reliable choice for directory creation.

  • mkdirp:

    mkdirp has been around for a long time and has a solid reputation, though its maintenance has slowed in favor of more modern alternatives like mkdirp-promise.

  • mkdirp-promise:

    mkdirp-promise is actively maintained and is gaining popularity due to its promise-based approach, making it a good choice for developers looking for modern solutions.

  • fs-extra:

    fs-extra is widely used and actively maintained, with a strong community backing and frequent updates, ensuring compatibility with the latest Node.js versions.

How to Choose: make-dir vs mkdirp vs mkdirp-promise vs fs-extra

  • make-dir:

    Opt for make-dir if you only need a simple way to create directories, especially when ensuring that parent directories are created as needed, without additional overhead.

  • mkdirp:

    Select mkdirp for a straightforward approach to creating nested directories, particularly if you prefer a callback-based API and do not require promises.

  • mkdirp-promise:

    Use mkdirp-promise if you prefer working with promises for creating directories, allowing for cleaner async code with async/await syntax.

  • fs-extra:

    Choose fs-extra if you need a comprehensive solution that extends the native fs module with additional methods like copy, move, and remove, making it ideal for complex file operations.

README for make-dir

make-dir

Make a directory and its parents if needed - Think mkdir -p

[!TIP] You probably want the built-in fsPromises.mkdir('…', {recursive: true}) instead.

Advantages over fsPromises.mkdir('…', {recursive: true})

  • Supports a custom fs implementation.

Advantages over mkdirp

  • Promise API (Async/await ready!)
  • Fixes many mkdirp issues
  • CI-tested on macOS, Linux, and Windows
  • Actively maintained
  • Doesn't bundle a CLI
  • Uses the native fs.mkdir/mkdirSync recursive option in Node.js unless overridden

Install

npm install make-dir

Usage

$ pwd
/Users/sindresorhus/fun
$ tree
.
import {makeDirectory} from 'make-dir';

const path = await makeDirectory('unicorn/rainbow/cake');

console.log(path);
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
$ tree
.
└── unicorn
    └── rainbow
        └── cake

Multiple directories:

import {makeDirectory} from 'make-dir';

const paths = await Promise.all([
	makeDirectory('unicorn/rainbow'),
	makeDirectory('foo/bar')
]);

console.log(paths);
/*
[
	'/Users/sindresorhus/fun/unicorn/rainbow',
	'/Users/sindresorhus/fun/foo/bar'
]
*/

API

makeDirectory(path, options?)

Returns a Promise for the path to the created directory.

makeDirectorySync(path, options?)

Returns the path to the created directory.

path

Type: string | URL

The directory to create.

options

Type: object

mode

Type: integer
Default: 0o777

The directory permissions.

fs

Type: object
Default: import fs from 'node:fs'

Use a custom fs implementation. For example graceful-fs.

Using a custom fs implementation will block the use of the native recursive option if fs.mkdir or fs.mkdirSync is not the native function.

Related