File system

fs/promises.mkdtemp()

Creates a uniquely named temporary directory by extending the supplied prefix. Tested on Node.js 22.16.0. Pass a prefix inside the intended parent, and delete only the directory returned by this invocation.

Formal Syntax & Structure

nodejs syntaxnodejs
fsPromises.mkdtemp(prefix[, options])

Parameters & Attributes

NameTypeDescription
prefixstring | Buffer | URLDirectory name prefix.
optionsstring | objectEncoding for returned path.

Return Value

Promise<string | Buffer>

Created directory path in requested encoding.

Working Example

Example Snippetnodejs
import * as fs from 'node:fs/promises';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir=await fs.mkdtemp(join(tmpdir(),'bytemind-ref-'));try {  const nested=await fs.mkdtemp(join(dir,'job-'));  console.log((await fs.stat(nested)).isDirectory(),nested.startsWith(join(dir,'job-')));} finally { await fs.rm(dir,{recursive:true,force:true}); }
Node.js Reference