CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tmp

Temporary file and directory creator for Node.js applications

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Tmp

Tmp 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.

Package Information

  • Package Name: tmp
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install tmp

Core Imports

const tmp = require("tmp");

For destructured imports:

const { file, fileSync, dir, dirSync, tmpName, tmpNameSync, setGracefulCleanup } = require("tmp");

Basic Usage

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

Architecture

The tmp library is built around several key concepts:

  • Dual API Pattern: Every operation has both asynchronous and synchronous versions
  • Crypto-based Security: Uses crypto.randomBytes for secure name generation with fallback to pseudo-random
  • Automatic Cleanup: Optional graceful cleanup on process exit via setGracefulCleanup()
  • Fine-grained Control: Comprehensive options for file modes, naming patterns, and descriptor management
  • Zero Dependencies: Self-contained implementation with no runtime dependencies

Capabilities

File Operations

Create and manage temporary files with full control over file descriptors, permissions, and cleanup behavior.

function file(options, callback);
function fileSync(options);

File Operations

Directory Operations

Create temporary directories with configurable cleanup policies including recursive removal of non-empty directories.

function dir(options, callback);
function dirSync(options);

Directory Operations

Name Generation

Generate unique temporary filenames without creating actual filesystem objects, useful for custom file creation workflows.

function tmpName(options, callback);
function tmpNameSync(options);

Name Generation

Cleanup Management

Control automatic cleanup behavior and access to the system temporary directory.

function setGracefulCleanup();
// Property getter for current temporary directory
tmp.tmpdir // string

Cleanup Management

Types

// 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
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/tmp@0.2.x
Publish Source
CLI
Badge
tessl/npm-tmp badge