Skip to content

Replacements for temp / tempy

fs.mkdtemp (native, since Node.js v14.x)

Node.js has the fs.mkdtemp function for creating a unique temporary directory. Directory cleanup can be done by passing {recursive: true} to fs.rm.

Example:

ts
import temp from 'temp'
import { mkdtemp, realpath } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'

const tempDirPath = temp.mkdirSync('foo') 
const tempDirPath = await mkdtemp(join(await realpath(tmpdir()), 'foo-')) 

fs.mkdtempDisposable (native, since Node.js v20.4.0)

Node.js now provides fs.mkdtempDisposable which leverages the using keyword for automatic cleanup. This eliminates the need for temp.track() or manual cleanup logic.

Example:

ts
import temp from 'temp'
import { mkdtempDisposable } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'

temp.track() 
const tempDirPath = temp.mkdirSync('foo') 
await using tempDir = await mkdtempDisposable(join(tmpdir(), 'foo-')) 
const tempDirPath = tempDir.path 

Deno

Deno provides built-in Deno.makeTempDir and Deno.makeTempFile for creating unique temporary directories and files in the system temp directory (or a custom dir). You can also set prefix and suffix. Both return the full path and require --allow-write.

ts
import { temporaryDirectory } from 'tempy'

const tempDir = temporaryDirectory({ prefix: 'foo-' }) 
const tempDir = await Deno.makeTempDir({ prefix: 'foo-' }) 
ts
import { temporaryFile } from 'tempy'

const tempFile = temporaryFile({ extension: 'txt' }) 
const tempFile = await Deno.makeTempFile({ suffix: '.txt' }) 

Released under the MIT License. (65e83b3b)