fs.mkdir(path[, options], callback)


异步地创建目录。

回调给出一个可能的异常和创建的第一个目录路径(如果 recursivetrue),(err, [path])

path 是已存在的目录时,调用 fs.mkdir() 仅在 recursive 为 false 时才导致错误。

// 创建 /tmp/a/apple,不管 `/tmp` 和 /tmp/a 是否存在。
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
  if (err) throw err;
});

在 Windows 上,即使使用递归,在根目录上使用 fs.mkdir() 也会导致错误:

fs.mkdir('/', { recursive: true }, (err) => {
  // => [Error: EPERM: operation not permitted, mkdir 'C:\']
});

另见: mkdir(2)

Asynchronously creates a directory.

The callback is given a possible exception and, if recursive is true, the first directory path created, (err, [path]).

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.

// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
fs.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:

fs.mkdir('/', { recursive: true }, (err) => {
  // => [Error: EPERM: operation not permitted, mkdir 'C:\']
});

See also: mkdir(2).