{"id":11338,"library":"mk-dirs","title":"Recursive Directory Creation Utility","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/mk-dirs","tags":["javascript","mkdir","make dir","recursive","mkdirp","typescript"],"install":[{"cmd":"npm install mk-dirs","lang":"bash","label":"npm"},{"cmd":"yarn add mk-dirs","lang":"bash","label":"yarn"},{"cmd":"pnpm add mk-dirs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the primary asynchronous API for ES Modules. It is a named export, not a default export.","wrong":"import mkdir from 'mk-dirs';","symbol":"mkdir (async ESM)","correct":"import { mkdir } from 'mk-dirs';"},{"note":"This is the primary asynchronous API for CommonJS environments. Ensure you destructure the 'mkdir' function from the module object.","wrong":"const mkdir = require('mk-dirs');","symbol":"mkdir (async CJS)","correct":"const { mkdir } = require('mk-dirs');"},{"note":"For synchronous operation, you must import specifically from `mk-dirs/sync`. Importing from the default path will yield the asynchronous API.","wrong":"import { mkdir } from 'mk-dirs';","symbol":"mkdir (sync ESM)","correct":"import { mkdir } from 'mk-dirs/sync';"}],"quickstart":{"code":"import { mkdir } from 'mk-dirs';\nimport { resolve } from 'path';\n\nasync function createDirectories() {\n  const baseDir = process.cwd();\n  console.log(`Current working directory: ${baseDir}`);\n\n  // Async/await usage\n  try {\n    let output1 = await mkdir('foo/bar/baz');\n    console.log(`Created directory (async/await): ${output1}`);\n\n    // Using `cwd` option\n    let customCwd = resolve(baseDir, 'temp/custom');\n    let output2 = await mkdir('alpha/beta', { cwd: customCwd });\n    console.log(`Created directory with custom cwd: ${output2}`);\n  } catch (err) {\n    console.error('Error during async/await mkdir:', err);\n  }\n\n  // Promise chain usage\n  mkdir('another/path/deeply')\n    .then(output => {\n      console.log(`Created directory (Promise chain): ${output}`);\n    })\n    .catch(err => {\n      console.error('Error during Promise chain mkdir:', err);\n    });\n}\n\ncreateDirectories();","lang":"typescript","description":"Demonstrates asynchronous recursive directory creation using both async/await and Promise chaining, including the use of a custom `cwd` option."},"warnings":[{"fix":"Consider using Node.js's native `fs.promises.mkdir` or `fs.mkdirSync` with `{ recursive: true }` for new projects or when migrating older code, unless `mk-dirs`'s specific features (like the 'cwd' option) are required.","message":"Native Node.js support for recursive directory creation makes external libraries like mk-dirs potentially redundant for simple use cases. Node.js v10.12.0 and above include `fs.mkdir` and `fs.mkdirSync` with a `{ recursive: true }` option, offering built-in functionality.","severity":"gotcha","affected_versions":">=10.12.0 (Node.js)"},{"fix":"To use the synchronous version, ensure your import statement is `import { mkdir } from 'mk-dirs/sync';` for ESM or `const { mkdir } = require('mk-dirs/sync');` for CommonJS.","message":"The synchronous API requires an explicit import path: `mk-dirs/sync`. Importing from the default `mk-dirs` will always provide the asynchronous, Promise-based API.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Always prefix octal numbers with '0o', e.g., `0o755` instead of `755`. The default is `0o777 & (~process.umask())`.","message":"Directory permissions (`options.mode`) must be specified in octal format, not decimal. Incorrectly formatted modes can lead to unexpected permissions or errors.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For ESM, ensure you use `import { mkdir } from 'mk-dirs';`. For CommonJS, use `const { mkdir } = require('mk-dirs');`.","cause":"Attempting to call the module object directly, or incorrect destructuring of the named 'mkdir' export in ESM or CJS.","error":"TypeError: (0 , mk_dirs__WEBPACK_IMPORTED_MODULE_0__.mkdir) is not a function"},{"fix":"Wrap async calls in a `try...catch` block (`await mkdir(...);`) or chain a `.catch(err => { /* handle error */ })` to the Promise return.","cause":"The `mkdir` function returns a Promise, and its rejection was not handled by an `await` within a `try/catch` block or a `.catch()` method.","error":"UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()."},{"fix":"Run the process with appropriate user permissions, verify the target directory is writable, or ensure the `mode` option is correctly specified in octal format (e.g., `0o777`).","cause":"Insufficient file system permissions for the current user to create directories at the specified path, or an incorrect `mode` option was applied.","error":"Error: EPERM: operation not permitted, mkdir '/path/to/dir'"}],"ecosystem":"npm"}