fsPromises.mkdtemp(prefix[, options])


创建唯一的临时目录。 通过在所提供的 prefix 的末尾附加六个随机字符来生成唯一的目录名称。 由于平台的不一致,请避免在 prefix 中尾随 X 字符。 某些平台,尤其是 BSD,可能返回六个以上的随机字符,并将 prefix 中的尾随 X 字符替换为随机字符。

可选的 options 参数可以是指定编码的字符串,也可以是具有 encoding 属性(指定要使用的字符编码)的对象。

import { mkdtemp } from 'node:fs/promises';

try {
  await mkdtemp(path.join(os.tmpdir(), 'foo-'));
} catch (err) {
  console.error(err);
}

fsPromises.mkdtemp() 方法会将六个随机选择的字符直接附加到 prefix 字符串。 例如,给定目录 /tmp,如果要在 /tmp 内创建临时目录,则 prefix 必须以平台特定的尾随路径分隔符(require('node:path').sep)结尾。

Creates a unique temporary directory. A unique directory name is generated by appending six random characters to the end of the provided prefix. Due to platform inconsistencies, avoid trailing X characters in prefix. Some platforms, notably the BSDs, can return more than six random characters, and replace trailing X characters in prefix with random characters.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use.

import { mkdtemp } from 'node:fs/promises';

try {
  await mkdtemp(path.join(os.tmpdir(), 'foo-'));
} catch (err) {
  console.error(err);
}

The fsPromises.mkdtemp() method will append the six randomly selected characters directly to the prefix string. For instance, given a directory /tmp, if the intention is to create a temporary directory within /tmp, the prefix must end with a trailing platform-specific path separator (require('node:path').sep).