sfcode
An Online Competing and Development Environment
|
Like mkdir -p
, but in Node.js!
Now with a modern API and no* bugs!
* may contain some bugs
Output (where /tmp/foo
already exists)
Or, if you don't have time to wait around for promises:
And now /tmp/foo/bar/baz exists, huzzah!
Create a new directory and any necessary subdirectories at dir
with octal permission string opts.mode
. If opts
is a string or number, it will be treated as the opts.mode
.
If opts.mode
isn't specified, it defaults to 0o777 & (~process.umask())
.
Promise resolves to first directory made
that had to be created, or undefined
if everything already exists. Promise rejects if any errors are encountered. Note that, in the case of promise rejection, some directories may have been created, as recursive directory creation is not an atomic operation.
You can optionally pass in an alternate fs
implementation by passing in opts.fs
. Your implementation should have opts.fs.mkdir(path, opts, cb)
and opts.fs.stat(path, cb)
.
You can also override just one or the other of mkdir
and stat
by passing in opts.stat
or opts.mkdir
, or providing an fs
option that only overrides one of these.
Synchronously create a new directory and any necessary subdirectories at dir
with octal permission string opts.mode
. If opts
is a string or number, it will be treated as the opts.mode
.
If opts.mode
isn't specified, it defaults to 0o777 & (~process.umask())
.
Returns the first directory that had to be created, or undefined if everything already exists.
You can optionally pass in an alternate fs
implementation by passing in opts.fs
. Your implementation should have opts.fs.mkdirSync(path, mode)
and opts.fs.statSync(path)
.
You can also override just one or the other of mkdirSync
and statSync
by passing in opts.statSync
or opts.mkdirSync
, or providing an fs
option that only overrides one of these.
Use the manual implementation (not the native one). This is the default when the native implementation is not available or the stat/mkdir implementation is overridden.
Use the native implementation (not the manual one). This is the default when the native implementation is available and stat/mkdir are not overridden.
On Node.js v10.12.0 and above, use the native fs.mkdir(p, {recursive:true})
option, unless fs.mkdir
/fs.mkdirSync
has been overridden by an option.
made
.fs.mkdir(path, { recursive: true })
(or fs.mkdirSync
)made
.fs.mkdir
implementation, with recursive: false
made
mkdir
errormade
mkdir
errorundefined
if a root dir, or made
if set, or path
On Windows file systems, attempts to create a root directory (ie, a drive letter or root UNC path) will fail. If the root directory exists, then it will fail with EPERM
. If the root directory does not exist, then it will fail with ENOENT
.
On posix file systems, attempts to create a root directory (in recursive mode) will succeed silently, as it is treated like just another directory that already exists. (In non-recursive mode, of course, it fails with EEXIST
.)
In order to preserve this system-specific behavior (and because it's not as if we can create the parent of a root directory anyway), attempts to create a root directory are passed directly to the fs
implementation, and any errors encountered are not handled.
The native implementation (as of at least Node.js v13.4.0) does not provide appropriate errors in some cases (see nodejs/node#31481 and nodejs/node#28015).
In order to work around this issue, the native implementation will fall back to the manual implementation if an ENOENT
error is encountered.
There are a few to choose from! Use the one that suits your needs best :D
ENOENT
as the error when some other problem is the actual cause.fs
methods in use.ENOENT
to achieve this.fs
methods in use.stat
calls when using the native implementation for this reason).ENOENT
errors for failures that are actually related to something other than a missing file system entry.fs
methods in use.fs
methods in use.Promise
language primitive. (Please don't. You deserve better.)This package also ships with a mkdirp
command.
With npm do:
to get the library locally, or
to get the command everywhere, or
to run the command without installing it globally.
This module works on node v8, but only v10 and above are officially supported, as Node v8 reached its LTS end of life 2020-01-01, which is in the past, as of this writing.
MIT