fs.mkdir(path[, options], callback)
path
<string> | <Buffer> | <URL>options
<Object> | <integer>callback
<Function>err
<Error>path
<string> | <undefined> 仅当创建目录时将recursive
设置为true
。
异步地创建目录。
回调给出一个可能的异常和创建的第一个目录路径(如果 recursive
为 true
),(err[, path])
。
当 recursive
为 true
时,如果没有创建目录,则 path
仍然为 undefined
。
可选的 options
参数可以是指定 mode
(权限和粘性位)的整数,也可以是具有 mode
属性和 recursive
属性(指示是否应创建父目录)的对象。
当 path
是已存在的目录时,调用 fs.mkdir()
仅在 recursive
为 false 时才导致错误。
import { mkdir } from 'node:fs';
// 创建 /tmp/a/apple,不管 `/tmp` 和 /tmp/a 是否存在。
mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});
在 Windows 上,即使使用递归,在根目录上使用 fs.mkdir()
也会导致错误:
import { mkdir } from 'node:fs';
mkdir('/', { recursive: true }, (err) => {
// => [Error: EPERM: operation not permitted, mkdir 'C:\']
});
有关更多详细信息,请参阅 POSIX mkdir(2)
文档。
path
<string> | <Buffer> | <URL>options
<Object> | <integer>callback
<Function>err
<Error>path
<string> | <undefined> Present only if a directory is created withrecursive
set totrue
.
Asynchronously creates a directory.
The callback is given a possible exception and, if recursive
is true
, the
first directory path created, (err[, path])
.
path
can still be undefined
when recursive
is true
, if no directory was
created.
The optional options
argument can be an integer specifying mode
(permission
and sticky bits), or an object with a mode
property and a recursive
property indicating whether parent directories should be created. Calling
fs.mkdir()
when path
is a directory that exists results in an error only
when recursive
is false.
import { mkdir } from 'node:fs';
// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});
On Windows, using fs.mkdir()
on the root directory even with recursion will
result in an error:
import { mkdir } from 'node:fs';
mkdir('/', { recursive: true }, (err) => {
// => [Error: EPERM: operation not permitted, mkdir 'C:\']
});
See the POSIX mkdir(2)
documentation for more details.