fsPromises.mkdtemp(prefix[, options])
prefix<string> | <Buffer> | <URL>options<string> | <Object>encoding<string> 默认值:'utf8'
- 返回:<Promise> 以包含新创建的临时目录文件系统路径的字符串形式完成。
创建一个唯一的临时目录。通过在提供的 prefix 后附加六个随机字符来生成唯一的目录名。由于平台差异,避免在 prefix 中使用尾随的 X 字符。一些平台,特别是 BSD 系统,可能会返回超过六个随机字符,并将 prefix 尾随的 X 字符替换为随机字符。
【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.】
可选的 options 参数可以是指定编码的字符串,或者是一个具有 encoding 属性的对象,用于指定要使用的字符编码。
【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';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
try {
await mkdtemp(join(tmpdir(), 'foo-'));
} catch (err) {
console.error(err);
} fsPromises.mkdtemp() 方法会将六个随机选取的字符直接附加到 prefix 字符串后。例如,给定一个目录 /tmp,如果目的是在 /tmp 内创建一个临时目录,prefix 必须以特定平台的路径分隔符(require('node:path').sep)结尾。
【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).】