npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

temp-file-context

v1.0.0

Published

Node functions for running code in self-cleaning temp file and directory contexts.

Readme

Temporary File Context

Typescript GPL 3.0-or-later

This is a zero dependency[^1] TypeScript implementation of Tamás Sallai's elegant and dependency-free solution for creating OS-agnostic, asynchronous, self-cleaning temporary file and directory contexts.[^2]

What is this for?

I got tired of implementing this pattern over and over: "Get a temporary directory name, make the directory, put a temp file in it, do stuff to the file, clean up the temporary directory"

While poking around for wrappers for temporary file operations, I found Tamás Sallai's article and it was exactly what I was looking for.

Installation

# npm
npm i temp-file-context

# pnpm
pnpm i temp-file-context

Usage

These are nonsense examples, but they show how withTempDir() and withTempFile() create a context in which you have access to a temporary directory and/or file.

The key thing to note is that once these functions exit, the temporary resources have already been destroyed; you don't need to do any additional cleanup.

Run some code within the context of a temporary directory

import { withTempDir } from "temp-file-context";
import fs from "node:fs";

withTempDir((dir) => {
  let processedFileCount = 0;
  for (let i = 1; i < 11; i++) {
    fs.writeFileSync(`${dir}/file-${i}.txt`, `This is file ${i}`);
  }
  // [Do whatever else you need to do with your temporary files here]

  return processedFileCount;
}).then((result) => {
  console.log("Files processed", result);
});

Use a temporary file

withTempFile() is very similar; it also creates a temporary directory, but instead of passing the directory name to your inner function, it passes a temporary filename within that directory.

When withTempFile() exits, it automatically destroys the temporary file and directory.

import { withTempFile } from "temp-file-context";
import fs from "node:fs";

withTempFile((filename) => {
  fs.writeFileSync(filename, "This is a temporary file");
  // [Do whatever you need to do with your temp file]
}).then(() => {
  console.log("Finished");
});

Running Tests

# npm
npm test

#pnpm
pnpm test

[^1]: Not counting development dependencies for TypeScript and testing [^2]: Sallai, T. (2020, April 7). Secure tempfiles in NodeJS without dependencies. Advanced Web Machinery. Retrieved September 23, 2026, from https://advancedweb.hu/secure-tempfiles-in-nodejs-without-dependencies/