node-temp

A Node.js utility for creating temporary files, directories, and write streams with automatic cleanup on exit.

Library
npm
v0.9.4
450stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
42/100Fair
Development Activity0
Maintenance20
Community68
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
48/100Fair
Architecture58
Code Quality50
Innovation30
Learning Curve55

temp is a lightweight Node.js library for working with temporary files and directories. It generates unique paths in the system’s temp directory, creates files and directories with sensible permissions, and offers an API modeled closely on Node’s built-in fs module so it feels familiar to use.

Beyond generating paths, temp adds optional automatic cleanup: calling track() registers a process exit handler that removes any tracked files and directories when the process ends, which is useful for CLI scripts and build tools that need to guarantee they don’t leave stray files behind. It supports callback- and Promise-based APIs for asynchronous operations, plus synchronous variants for scripts that don’t need async flow.

What You Get

  • Unique path generation - temp.path() builds collision-resistant paths using a timestamp, the process PID, and a random suffix
  • Callback, Promise, and sync APIs - mkdir/mkdirSync and open/openSync support both callback style and Promises depending on whether a callback is passed
  • Automatic cleanup on exit - track() attaches a process ‘exit’ listener that removes all tracked temp files/dirs via rimraf
  • WriteStream support - createWriteStream() returns a standard fs.WriteStream pointed at a generated temp path, tracked for cleanup like other resources

Common Use Cases

  • CLI tool scratch space - command-line utilities that need a working directory for intermediate output and want it removed automatically when the process exits
  • Test suite fixtures - test suites that need throwaway files or directories per test case without manually tracking paths for cleanup
  • Build tool intermediate output - build tools and Grunt-style tasks that generate temporary artifacts during a build step
  • Piping data to external processes - scripts that write data to a file so it can be passed as a path argument to a spawned external program

Under The Hood

Architecture node-temp is implemented as a single CommonJS module (lib/temp.js) that exports a small set of functions directly, with no classes or instances. Tracking state (filesToDelete, dirsToDelete, a tracking boolean) lives as module-level closures rather than being encapsulated in an object, and a promisify() helper bridges the callback-based core into a dual callback/Promise API for mkdir and open. Directory creation and recursive removal are delegated outward to mkdirp and rimraf rather than reimplemented, keeping the module itself thin. The design is simple and easy to trace end to end, though the shared mutable module state means all callers in a process share one tracking list.

Tech Stack Plain JavaScript (CommonJS, no build step or TypeScript), depending on rimraf (~2.6.2) for recursive directory removal and mkdirp (^0.5.1) for directory creation. Tests run under Mocha (devDependency, pinned to 6.2.3) invoked via npm test. The package declares Node >=6.0.0 compatibility and is exercised across platforms and Node versions via both Travis CI and AppVeyor (for Windows coverage).

Code Quality test/temp-test.js uses Mocha with Node’s built-in assert module and covers the core happy paths — mkdir, open, createWriteStream, path generation, and cleanupSync — but has limited coverage of error/edge cases beyond that. There is no TypeScript, no bundled type declarations, and no linter or formatter configuration in the repo, though the README documents an informal TypeScript interface for consumers. Cross-platform CI (Travis + AppVeyor) is the main quality signal beyond the test suite itself.

What Makes It Unique node-temp doesn’t introduce a novel technique — it’s a conventional wrapper around fs, mkdirp, and rimraf — but its value is in the ergonomics: an fs-like API, opt-in (rather than automatic) exit-time cleanup aimed specifically at avoiding surprises in long-running server processes, and a consistent affix (prefix/suffix/dir) system shared across all of its file and directory creation functions.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search