Temporary file and directory creator for Node.js applications
—
Temporary directory creation and management with configurable cleanup policies including recursive removal of non-empty directories.
Creates a temporary directory asynchronously.
/**
* Creates a temporary directory
* @param {Options|Function} options - Configuration options or callback function
* @param {Function} callback - Callback function (err, path, cleanupCallback) => void
*/
function dir(options, callback);Callback Parameters:
err (Error|null): Error object or null if successfulpath (string): Full path to the created temporary directorycleanupCallback (Function): Function to manually remove the directoryUsage Examples:
const tmp = require("tmp");
// Basic directory creation
tmp.dir(function(err, path, cleanupCallback) {
if (err) throw err;
console.log("Temp directory:", path);
// Use the directory - create files, subdirectories, etc.
const fs = require("fs");
fs.writeFileSync(path + "/test.txt", "test content");
// Clean up when done
cleanupCallback();
});
// Directory creation with custom options
tmp.dir({
mode: 0o755,
prefix: "myapp-workspace-",
unsafeCleanup: true // Allow removal even if not empty
}, function(err, path, cleanupCallback) {
if (err) throw err;
console.log("Custom temp directory:", path);
// Create nested structure
const fs = require("fs");
fs.mkdirSync(path + "/subdir");
fs.writeFileSync(path + "/subdir/file.txt", "nested content");
// This will work because unsafeCleanup is true
cleanupCallback();
});
// Template-based directory naming
tmp.dir({ template: "build-XXXXXX" }, function(err, path, cleanupCallback) {
if (err) throw err;
console.log("Template directory:", path);
// Path will be like: /tmp/build-abc123
cleanupCallback();
});Creates a temporary directory synchronously.
/**
* Creates a temporary directory synchronously
* @param {Options} options - Configuration options
* @returns {DirSyncObject} Object with name and removeCallback
* @throws {Error} If directory creation fails
*/
function dirSync(options);Return Value:
interface DirSyncObject {
name: string; // Full path to the temporary directory
removeCallback: Function; // Cleanup function
}Usage Examples:
const tmp = require("tmp");
const fs = require("fs");
// Basic synchronous directory creation
const tmpDir = tmp.dirSync();
console.log("Temp directory:", tmpDir.name);
// Use the directory
fs.writeFileSync(tmpDir.name + "/data.json", JSON.stringify({test: true}));
// Clean up
tmpDir.removeCallback();
// Directory with custom permissions and naming
const workDir = tmp.dirSync({
mode: 0o750,
prefix: "workspace-",
postfix: "-tmp"
});
console.log("Work directory:", workDir.name);
// Create project structure
fs.mkdirSync(workDir.name + "/src");
fs.mkdirSync(workDir.name + "/dist");
fs.writeFileSync(workDir.name + "/package.json", "{}");
workDir.removeCallback();
// Template directory in custom location
const buildDir = tmp.dirSync({
template: "build-XXXXXX",
dir: "builds", // Create in /tmp/builds/build-XXXXXX
unsafeCleanup: true
});
// Create build artifacts
fs.writeFileSync(buildDir.name + "/output.js", "// generated code");
fs.mkdirSync(buildDir.name + "/assets");
buildDir.removeCallback(); // Will remove even with contents due to unsafeCleanupBy default, directories can only be removed if they are empty. Use the unsafeCleanup option for recursive removal:
tmp.dir(function(err, path, cleanupCallback) {
const fs = require("fs");
// Create some content
fs.writeFileSync(path + "/file.txt", "content");
// This will fail because directory is not empty
cleanupCallback(function(err) {
if (err) {
console.log("Cleanup failed:", err.message);
// You need to manually empty the directory first
fs.unlinkSync(path + "/file.txt");
cleanupCallback(); // Now this will work
}
});
});tmp.dir({ unsafeCleanup: true }, function(err, path, cleanupCallback) {
const fs = require("fs");
// Create nested content
fs.mkdirSync(path + "/subdir");
fs.writeFileSync(path + "/subdir/file.txt", "content");
// This will succeed and remove everything recursively
cleanupCallback();
});All directory operations support the following options:
interface DirectoryOptions {
mode?: number; // Directory permissions (default: 0o700)
prefix?: string; // Directory name prefix (default: "tmp")
postfix?: string; // Directory name postfix
template?: string; // Template with XXXXXX (e.g., "temp-XXXXXX")
name?: string; // Fixed directory name (relative to dir/tmpdir)
dir?: string; // Parent directory within tmpdir
tmpdir?: string; // Override system temp directory
tries?: number; // Max naming attempts (default: 3)
keep?: boolean; // Skip automatic cleanup
unsafeCleanup?: boolean; // Allow recursive removal of non-empty directories
}Important Notes:
0o700 (owner access only) for securityunsafeCleanup: true, cleanup will fail if the directory contains any files or subdirectoriesunsafeCleanup option uses recursive removal similar to rm -rfunsafeCleanup, be very careful as it will remove all contents without confirmationXXXXXX sequence gets replacedname option must not contain path separators and cannot be absolute pathsInstall with Tessl CLI
npx tessl i tessl/npm-tmp