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 install tmp
yarn add tmp
pnpm add tmp
Import
import tmp from 'tmp';
Quick Example
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-67890About 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
| Package | tmp |
| Category | File System |
| Weekly Downloads | 10M+ |
| License | MIT |
| Install | npm install tmp |
Related Packages
fs-extra is a drop-in replacement for Node.js's built-in fs module that adds additional file system …
mkdirp is a Node.js utility that recursively creates directories, equivalent to mkdir -p in Unix. Gi…
rimraf is a Node.js implementation of the Unix rm -rf command, providing cross-platform recursive di…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.