📂
File System10M+/wkMIT

tmp

tmp is a library for creating temporary files and directories in Node.js that provides automatic cleanup capabilities. The library creates temporary files and d

Installation

npm
npm install tmp
yarn
yarn add tmp
pnpm
pnpm add tmp

Import

ESM
import tmp from 'tmp';

Quick Example

usage
import tmp from 'tmp';

tmp.setGracefulCleanup();

const tmpFile = tmp.fileSync({ postfix: '.json' });
console.log(tmpFile.name); // /tmp/tmp-12345.json
tmpFile.removeCallback();

const tmpDir = tmp.dirSync({ unsafeCleanup: true });
console.log(tmpDir.name); // /tmp/tmp-67890

About tmp

tmp is a library for creating temporary files and directories in Node.js that provides automatic cleanup capabilities. The library creates temporary files and directories in the system's default temp directory (os.tmpdir()) with unique names generated using cryptographic randomness. tmp supports automatic cleanup through two mechanisms: graceful cleanup that removes temporary files when the process exits normally, and explicit cleanup through a callback function returned with each created temp file/directory. Configuration options include prefix and postfix for naming, directory for custom temp location, keep for preserving files after process exit, mode for file permissions, and tries for controlling retry attempts when name collisions occur. The library provides both callback-based and promise-based APIs (through tmp-promise). tmp handles the common need for temporary workspace in build tools, test suites, file upload processing, and data transformation pipelines where intermediate files need a safe location that is cleaned up automatically. The library's unsafeCleanup option allows deleting non-empty temporary directories. tmp is used by numerous build tools, test runners, and CLI applications that need temporary file storage with reliable cleanup.

Quick Facts

Packagetmp
CategoryFile System
Weekly Downloads10M+
LicenseMIT
Installnpm install tmp

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.