CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tmp

Temporary file and directory creator for Node.js applications

Pending
Overview
Eval results
Files

directory-operations.mddocs/

Directory Operations

Temporary directory creation and management with configurable cleanup policies including recursive removal of non-empty directories.

Capabilities

Asynchronous Directory Creation

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 successful
  • path (string): Full path to the created temporary directory
  • cleanupCallback (Function): Function to manually remove the directory

Usage 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();
});

Synchronous Directory Creation

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 unsafeCleanup

Directory Cleanup Behavior

By default, directories can only be removed if they are empty. Use the unsafeCleanup option for recursive removal:

Safe Cleanup (Default)

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
    }
  });
});

Unsafe Cleanup (Recursive)

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();
});

Directory Options

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:

  • Directory permissions default to 0o700 (owner access only) for security
  • Without unsafeCleanup: true, cleanup will fail if the directory contains any files or subdirectories
  • The unsafeCleanup option uses recursive removal similar to rm -rf
  • When using unsafeCleanup, be very careful as it will remove all contents without confirmation
  • Template patterns work the same as with files - exactly one XXXXXX sequence gets replaced
  • Fixed names via the name option must not contain path separators and cannot be absolute paths

Install with Tessl CLI

npx tessl i tessl/npm-tmp

docs

cleanup-management.md

directory-operations.md

file-operations.md

index.md

name-generation.md

tile.json