Temporary file and directory creator for Node.js applications
npx @tessl/cli install tessl/npm-tmp@0.2.0Tmp is a widely-used Node.js library for creating temporary files and directories with automatic cleanup capabilities. It provides both asynchronous and synchronous APIs for secure temporary filesystem object creation using crypto-based randomness with configurable options and comprehensive cleanup control.
npm install tmpconst tmp = require("tmp");For destructured imports:
const { file, fileSync, dir, dirSync, tmpName, tmpNameSync, setGracefulCleanup } = require("tmp");const tmp = require("tmp");
// Enable automatic cleanup on process exit
tmp.setGracefulCleanup();
// Create a temporary file asynchronously
tmp.file(function(err, path, fd, cleanupCallback) {
if (err) throw err;
console.log("Temp file created:", path);
console.log("File descriptor:", fd);
// Use the file...
// Manual cleanup (optional - happens automatically if setGracefulCleanup was called)
cleanupCallback();
});
// Create a temporary directory synchronously
const tmpDir = tmp.dirSync();
console.log("Temp directory:", tmpDir.name);
// Use the directory...
// Manual cleanup
tmpDir.removeCallback();The tmp library is built around several key concepts:
Create and manage temporary files with full control over file descriptors, permissions, and cleanup behavior.
function file(options, callback);
function fileSync(options);Create temporary directories with configurable cleanup policies including recursive removal of non-empty directories.
function dir(options, callback);
function dirSync(options);Generate unique temporary filenames without creating actual filesystem objects, useful for custom file creation workflows.
function tmpName(options, callback);
function tmpNameSync(options);Control automatic cleanup behavior and access to the system temporary directory.
function setGracefulCleanup();// Property getter for current temporary directory
tmp.tmpdir // string// Options object for all operations
interface Options {
keep?: boolean; // Skip garbage collection
tries?: number; // Max attempts for name generation (default: 3)
mode?: number; // File/directory permissions (default: 0o600/0o700)
template?: string; // mkstemp-like template with XXXXXX pattern
name?: string; // Fixed name relative to tmpdir or dir
dir?: string; // Directory relative to root tmpdir
prefix?: string; // Name prefix (default: "tmp")
postfix?: string; // Name postfix
tmpdir?: string; // Override OS temp directory
unsafeCleanup?: boolean; // Recursively remove non-empty directories
detachDescriptor?: boolean; // Caller manages file descriptor
discardDescriptor?: boolean; // Close file descriptor immediately
}
// Return object for fileSync
interface FileSyncObject {
name: string; // Path to temporary file
fd: number; // File descriptor (-1 if discarded)
removeCallback: Function; // Cleanup function
}
// Return object for dirSync
interface DirSyncObject {
name: string; // Path to temporary directory
removeCallback: Function; // Cleanup function
}