Closes the file handle (if open) and deletes the file path.
@eryx/tempfile Module
JSON
Temporary files and directories.
open() returns a file-like object with an explicit :close() method.
openDir() returns a directory handle with path and :close().
Warning
Temporary resources are explicit-lifecycle. You must call :close()
on returned handles to ensure cleanup.
Unlike some runtimes, this module does not rely on GC finalizers.
Summary
Classes
Functions
API Reference
Classes
TempFile
Properties
Absolute path to the temp file.
The underlying open file
TempFile:close
TempDir
Represents a temporary directory handle.
Use path for filesystem operations and call :close() to
remove it recursively.
Properties
Absolute path to the temp directory.
TempDir:close
Removes the directory recursively.
Functions
TempFile:close
TempDir:close
tempfile.tempdir
Returns the process temporary directory.
Returns
string Absolute path to the OS temp directory.
tempfile.mktemp
Creates a unique temp file, then immediately closes and deletes it.
This is useful when you only need a collision-resistant path string.
Parameters
string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.
Returns
string A unique temp file path that does not exist on return.
tempfile.open
Creates and opens a unique temp file.
The returned handle behaves like fs.File and also supports :close()
to close and delete the file path.
Parameters
string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.
string? File mode ("r", "w", "a", "r+", "w+", "a+"). Defaults to "w+".
Returns
TempFile Open temp file handle with explicit cleanup methods.
tempfile.openDir
Creates a unique temporary directory.
Parameters
string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.
Returns
TempDir Directory handle with path and explicit cleanup methods.
tempfile.withTempFile
Creates a temp file, calls fn(file), and always cleans it up.
file:close() is guaranteed after callback completion, including error
paths. Any callback error is rethrown.
Parameters
(file: TempFile) -> T Callback that uses the temp file handle.
Returns
T The callback return value.
tempfile.withTempDir
Creates a temp directory, calls fn(path), and always removes it.
Directory cleanup is guaranteed after callback completion, including error paths. Any callback error is rethrown.
Parameters
(path: string) -> T Callback that receives the temp directory path.
string? Optional name/template prefix.
Returns
T The callback return value.