8015379441719ee4435bcbc0c622823746ba8a07589ec55bffd2ba529e70dd79092b3d637b0a641e8e6559e1de77d1b249064ca34692e71e08ed7f3a70c987 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # mkdirp
  2. Like `mkdir -p`, but in Node.js!
  3. Now with a modern API and no\* bugs!
  4. <small>\* may contain some bugs</small>
  5. # example
  6. ## pow.js
  7. ```js
  8. // hybrid module, import or require() both work
  9. import { mkdirp } from 'mkdirp'
  10. // or:
  11. const { mkdirp } = require('mkdirp')
  12. // return value is a Promise resolving to the first directory created
  13. mkdirp('/tmp/foo/bar/baz').then(made =>
  14. console.log(`made directories, starting with ${made}`)
  15. )
  16. ```
  17. Output (where `/tmp/foo` already exists)
  18. ```
  19. made directories, starting with /tmp/foo/bar
  20. ```
  21. Or, if you don't have time to wait around for promises:
  22. ```js
  23. import { mkdirp } from 'mkdirp'
  24. // return value is the first directory created
  25. const made = mkdirp.sync('/tmp/foo/bar/baz')
  26. console.log(`made directories, starting with ${made}`)
  27. ```
  28. And now /tmp/foo/bar/baz exists, huzzah!
  29. # methods
  30. ```js
  31. import { mkdirp } from 'mkdirp'
  32. ```
  33. ## `mkdirp(dir: string, opts?: MkdirpOptions) => Promise<string | undefined>`
  34. Create a new directory and any necessary subdirectories at `dir`
  35. with octal permission string `opts.mode`. If `opts` is a string
  36. or number, it will be treated as the `opts.mode`.
  37. If `opts.mode` isn't specified, it defaults to `0o777`.
  38. Promise resolves to first directory `made` that had to be
  39. created, or `undefined` if everything already exists. Promise
  40. rejects if any errors are encountered. Note that, in the case of
  41. promise rejection, some directories _may_ have been created, as
  42. recursive directory creation is not an atomic operation.
  43. You can optionally pass in an alternate `fs` implementation by
  44. passing in `opts.fs`. Your implementation should have
  45. `opts.fs.mkdir(path, opts, cb)` and `opts.fs.stat(path, cb)`.
  46. You can also override just one or the other of `mkdir` and `stat`
  47. by passing in `opts.stat` or `opts.mkdir`, or providing an `fs`
  48. option that only overrides one of these.
  49. ## `mkdirp.sync(dir: string, opts: MkdirpOptions) => string|undefined`
  50. Synchronously create a new directory and any necessary
  51. subdirectories at `dir` with octal permission string `opts.mode`.
  52. If `opts` is a string or number, it will be treated as the
  53. `opts.mode`.
  54. If `opts.mode` isn't specified, it defaults to `0o777`.
  55. Returns the first directory that had to be created, or undefined
  56. if everything already exists.
  57. You can optionally pass in an alternate `fs` implementation by
  58. passing in `opts.fs`. Your implementation should have
  59. `opts.fs.mkdirSync(path, mode)` and `opts.fs.statSync(path)`.
  60. You can also override just one or the other of `mkdirSync` and
  61. `statSync` by passing in `opts.statSync` or `opts.mkdirSync`, or
  62. providing an `fs` option that only overrides one of these.
  63. ## `mkdirp.manual`, `mkdirp.manualSync`
  64. Use the manual implementation (not the native one). This is the
  65. default when the native implementation is not available or the
  66. stat/mkdir implementation is overridden.
  67. ## `mkdirp.native`, `mkdirp.nativeSync`
  68. Use the native implementation (not the manual one). This is the
  69. default when the native implementation is available and
  70. stat/mkdir are not overridden.
  71. # implementation
  72. On Node.js v10.12.0 and above, use the native `fs.mkdir(p,
  73. {recursive:true})` option, unless `fs.mkdir`/`fs.mkdirSync` has
  74. been overridden by an option.
  75. ## native implementation
  76. - If the path is a root directory, then pass it to the underlying
  77. implementation and return the result/error. (In this case,
  78. it'll either succeed or fail, but we aren't actually creating
  79. any dirs.)
  80. - Walk up the path statting each directory, to find the first
  81. path that will be created, `made`.
  82. - Call `fs.mkdir(path, { recursive: true })` (or `fs.mkdirSync`)
  83. - If error, raise it to the caller.
  84. - Return `made`.
  85. ## manual implementation
  86. - Call underlying `fs.mkdir` implementation, with `recursive:
  87. false`
  88. - If error:
  89. - If path is a root directory, raise to the caller and do not
  90. handle it
  91. - If ENOENT, mkdirp parent dir, store result as `made`
  92. - stat(path)
  93. - If error, raise original `mkdir` error
  94. - If directory, return `made`
  95. - Else, raise original `mkdir` error
  96. - else
  97. - return `undefined` if a root dir, or `made` if set, or `path`
  98. ## windows vs unix caveat
  99. On Windows file systems, attempts to create a root directory (ie,
  100. a drive letter or root UNC path) will fail. If the root
  101. directory exists, then it will fail with `EPERM`. If the root
  102. directory does not exist, then it will fail with `ENOENT`.
  103. On posix file systems, attempts to create a root directory (in
  104. recursive mode) will succeed silently, as it is treated like just
  105. another directory that already exists. (In non-recursive mode,
  106. of course, it fails with `EEXIST`.)
  107. In order to preserve this system-specific behavior (and because
  108. it's not as if we can create the parent of a root directory
  109. anyway), attempts to create a root directory are passed directly
  110. to the `fs` implementation, and any errors encountered are not
  111. handled.
  112. ## native error caveat
  113. The native implementation (as of at least Node.js v13.4.0) does
  114. not provide appropriate errors in some cases (see
  115. [nodejs/node#31481](https://github.com/nodejs/node/issues/31481)
  116. and
  117. [nodejs/node#28015](https://github.com/nodejs/node/issues/28015)).
  118. In order to work around this issue, the native implementation
  119. will fall back to the manual implementation if an `ENOENT` error
  120. is encountered.
  121. # choosing a recursive mkdir implementation
  122. There are a few to choose from! Use the one that suits your
  123. needs best :D
  124. ## use `fs.mkdir(path, {recursive: true}, cb)` if:
  125. - You wish to optimize performance even at the expense of other
  126. factors.
  127. - You don't need to know the first dir created.
  128. - You are ok with getting `ENOENT` as the error when some other
  129. problem is the actual cause.
  130. - You can limit your platforms to Node.js v10.12 and above.
  131. - You're ok with using callbacks instead of promises.
  132. - You don't need/want a CLI.
  133. - You don't need to override the `fs` methods in use.
  134. ## use this module (mkdirp 1.x or 2.x) if:
  135. - You need to know the first directory that was created.
  136. - You wish to use the native implementation if available, but
  137. fall back when it's not.
  138. - You prefer promise-returning APIs to callback-taking APIs.
  139. - You want more useful error messages than the native recursive
  140. mkdir provides (at least as of Node.js v13.4), and are ok with
  141. re-trying on `ENOENT` to achieve this.
  142. - You need (or at least, are ok with) a CLI.
  143. - You need to override the `fs` methods in use.
  144. ## use [`make-dir`](http://npm.im/make-dir) if:
  145. - You do not need to know the first dir created (and wish to save
  146. a few `stat` calls when using the native implementation for
  147. this reason).
  148. - You wish to use the native implementation if available, but
  149. fall back when it's not.
  150. - You prefer promise-returning APIs to callback-taking APIs.
  151. - You are ok with occasionally getting `ENOENT` errors for
  152. failures that are actually related to something other than a
  153. missing file system entry.
  154. - You don't need/want a CLI.
  155. - You need to override the `fs` methods in use.
  156. ## use mkdirp 0.x if:
  157. - You need to know the first directory that was created.
  158. - You need (or at least, are ok with) a CLI.
  159. - You need to override the `fs` methods in use.
  160. - You're ok with using callbacks instead of promises.
  161. - You are not running on Windows, where the root-level ENOENT
  162. errors can lead to infinite regress.
  163. - You think vinyl just sounds warmer and richer for some weird
  164. reason.
  165. - You are supporting truly ancient Node.js versions, before even
  166. the advent of a `Promise` language primitive. (Please don't.
  167. You deserve better.)
  168. # cli
  169. This package also ships with a `mkdirp` command.
  170. ```
  171. $ mkdirp -h
  172. usage: mkdirp [DIR1,DIR2..] {OPTIONS}
  173. Create each supplied directory including any necessary parent directories
  174. that don't yet exist.
  175. If the directory already exists, do nothing.
  176. OPTIONS are:
  177. -m<mode> If a directory needs to be created, set the mode as an octal
  178. --mode=<mode> permission string.
  179. -v --version Print the mkdirp version number
  180. -h --help Print this helpful banner
  181. -p --print Print the first directories created for each path provided
  182. --manual Use manual implementation, even if native is available
  183. ```
  184. # install
  185. With [npm](http://npmjs.org) do:
  186. ```
  187. npm install mkdirp
  188. ```
  189. to get the library locally, or
  190. ```
  191. npm install -g mkdirp
  192. ```
  193. to get the command everywhere, or
  194. ```
  195. npx mkdirp ...
  196. ```
  197. to run the command without installing it globally.
  198. # platform support
  199. This module works on node v8, but only v10 and above are officially
  200. supported, as Node v8 reached its LTS end of life 2020-01-01, which is in
  201. the past, as of this writing.
  202. # license
  203. MIT