JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 45423
  • Score
    100M100P100Q150025F
  • License MIT

A tiny (420B) utility to make a directory and its parents, recursively

Package Exports

  • mk-dirs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mk-dirs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mk-dirs Build Status

A tiny (420B) utility to make a directory and its parents, recursively

This is a Promise-based utility that recursively creates directories.
It's effectively mkdir -p for Node.js

This module is a fast and lightweight alternative to mkdirp.
Check out Comparisons for more info!

Important: Requires Node 8.x or later – uses async functions.

Available in these formats:

  • ES Module: dist/index.mjs
  • CommonJS: dist/index.js

Note:
Are you using Node.js 10.12 or later?
If so, You should use the built-in fs.mkdir instead!

const { mkdir } = require('fs');
const { promisify } = require('util');

const mkdirp = promisify(mkdir);

function mkdirs(str, opts={}) {
  return mkdirp(str, { ...opts, recursive:true });
}

Install

$ npm install --save mk-dirs

Usage

$ pwd
# /Users/hello/world

$ tree
# .
import mkdirs from 'mk-dirs';
import { resolve } from 'path';

// Async/await
try {
  let output = await mkdirs('foo/bar/baz');
  console.log(output); //=> "/Users/hello/world/foo/bar/baz"
} catch (err) {
  //
}

// Promises
mkdirs('foo/bar/baz').then(output => {
  console.log(output); //=> "/Users/hello/world/foo/bar/baz"
}).catch(err => {
  //
});

// Using `cwd` option
let dir = resolve('foo/bar');
await mkdirs('hola/mundo', { cwd: dir });
//=> "/Users/hello/world/foo/bar/hola/mundo"
$ tree
# .
# └── foo
#     └── bar
#         └── baz
#         └── hola
#             └── mundo

API

mkdir(path, options={})

Returns: Promise<String>

Returns a Promise, which resolves with the full path (string) of the created directory.
Any file system errors will be thrown and must be caught manually.

path

Type: String

The directory to create.

options.cwd

Type: String
Default: .

The directory to resolve your path from.
Defaults to the process.cwd() – aka, the directory that your command is run within.

options.mode

Type: Number
Default: 0o777 & (~process.umask())

The directory permissions to set.

Important: Must be in octal format!

Comparisons

Versus make-dir

  • mk-dirs is slightly faster
  • ...has zero dependencies
  • ...does offer cwd option
  • ...does not re-wrap an existing Promise
  • ...does not ship with a sync method
  • ...does not allow custom fs option

Versus mkdirp

  • mk-dirs is much faster
  • ...has zero dependencies
  • ...is a Promise-based API
  • ...is async/await ready!
  • ...is tested on macOS, Linux, and Windows
  • ... has fixes for mkdirp issues: #96, #70, #66
  • ...includes a cwd option
  • ...does not ship with a sync method
  • ...does not allow custom fs option
  • ...does not bundle a CLI runtime
  • premove – A tiny (247B) utility to remove items recursively

License

MIT © Luke Edwards