| description | Modern alternatives to the temp and tempy packages for creating temporary files and directories |
|---|
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:
import temp from 'temp' // [!code --]
import { mkdtemp, realpath } from 'node:fs/promises' // [!code ++]
import { join } from 'node:path' // [!code ++]
import { tmpdir } from 'node:os' // [!code ++]
const tempDirPath = temp.mkdirSync('foo') // [!code --]
const tempDirPath = await mkdtemp(join(await realpath(tmpdir()), 'foo-')) // [!code ++]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:
import temp from 'temp' // [!code --]
import { mkdtempDisposable } from 'node:fs/promises' // [!code ++]
import { join } from 'node:path' // [!code ++]
import { tmpdir } from 'node:os' // [!code ++]
temp.track() // [!code --]
const tempDirPath = temp.mkdirSync('foo') // [!code --]
await using tempDir = await mkdtempDisposable(join(tmpdir(), 'foo-')) // [!code ++]
const tempDirPath = tempDir.path // [!code ++]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.
import { temporaryDirectory } from 'tempy' // [!code --]
const tempDir = temporaryDirectory({ prefix: 'foo-' }) // [!code --]
const tempDir = await Deno.makeTempDir({ prefix: 'foo-' }) // [!code ++]import { temporaryFile } from 'tempy' // [!code --]
const tempFile = temporaryFile({ extension: 'txt' }) // [!code --]
const tempFile = await Deno.makeTempFile({ suffix: '.txt' }) // [!code ++]