Recursive Directory Creation Utility

3.0.0 · active · verified Sun Apr 19

mk-dirs is a minimalistic, Promise-based utility designed for recursively creating directories in Node.js, functioning as an `mkdir -p` equivalent. Currently stable at version 3.0.0, it differentiates itself by being exceptionally lightweight (381B to 419B gzipped) and having zero external dependencies, offering a faster alternative to packages like `mkdirp` and `make-dir`. It provides both an asynchronous (default) and a synchronous opt-in mode, catering to different application needs and Node.js versions (>=8.x for async, >=6.x for sync). While Node.js v10.12.0+ includes native `fs.mkdir` with a `recursive` option, `mk-dirs` maintains its value through a consistent, promise-based API and `cwd` option. Its release cadence is not explicitly stated but typically follows a "release when needed" pattern for such focused utilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates asynchronous recursive directory creation using both async/await and Promise chaining, including the use of a custom `cwd` option.

import { mkdir } from 'mk-dirs';
import { resolve } from 'path';

async function createDirectories() {
  const baseDir = process.cwd();
  console.log(`Current working directory: ${baseDir}`);

  // Async/await usage
  try {
    let output1 = await mkdir('foo/bar/baz');
    console.log(`Created directory (async/await): ${output1}`);

    // Using `cwd` option
    let customCwd = resolve(baseDir, 'temp/custom');
    let output2 = await mkdir('alpha/beta', { cwd: customCwd });
    console.log(`Created directory with custom cwd: ${output2}`);
  } catch (err) {
    console.error('Error during async/await mkdir:', err);
  }

  // Promise chain usage
  mkdir('another/path/deeply')
    .then(output => {
      console.log(`Created directory (Promise chain): ${output}`);
    })
    .catch(err => {
      console.error('Error during Promise chain mkdir:', err);
    });
}

createDirectories();

view raw JSON →