Find Cache DirIntroduction

Introduction

Find the common standard cache directory across platforms

Last updated:

@visulima/find-cache-dir

Find the standard cache directory for your application across different operating systems. Automatically locates platform-specific cache locations and creates namespaced directories for your project.

Key Features

Platform-Aware

  • macOS: ~/Library/Caches/
  • Windows: %LOCALAPPDATA% or %USERPROFILE%\AppData\Local\
  • Linux: ~/.cache/ or $XDG_CACHE_HOME

Flexible Options

  • Async and sync versions
  • Automatic directory creation
  • Custom starting directory
  • Error handling controls

Project-Scoped

  • Creates node_modules/.cache/your-app structure
  • Follows Node.js caching conventions
  • Isolated per-project caching

Quick Start

Install the package:

npm install @visulima/find-cache-dir

Find your cache directory:

import { findCacheDir } from "@visulima/find-cache-dir";

const cacheDir = await findCacheDir("my-app");
console.log(cacheDir);
// macOS: /Users/username/Library/Caches/my-app
// Linux: /home/username/.cache/my-app
// Windows: C:\Users\username\AppData\Local\my-app

Use Cases

Cache Build Artifacts

Store compiled output for faster rebuilds:

import { findCacheDir } from "@visulima/find-cache-dir";
import { writeFileSync } from "fs";
import { join } from "path";

const cacheDir = await findCacheDir("my-compiler", { create: true });
const outputPath = join(cacheDir, "compiled.js");

writeFileSync(outputPath, compiledCode);
console.log(`Cached to: ${outputPath}`);

Store Temporary Downloads

Cache downloaded files between runs:

import { findCacheDir } from "@visulima/find-cache-dir";
import { createWriteStream, existsSync } from "fs";
import { join } from "path";

async function downloadWithCache(url: string, filename: string) {
  const cacheDir = await findCacheDir("my-downloader", { create: true });
  const cachePath = join(cacheDir, filename);

  if (existsSync(cachePath)) {
    console.log("Using cached file");
    return cachePath;
  }

  // Download and cache
  const response = await fetch(url);
  const fileStream = createWriteStream(cachePath);
  // ... pipe response to file ...

  return cachePath;
}

Share Cache Across Tools

Multiple tools can share a cache namespace:

import { findCacheDir } from "@visulima/find-cache-dir";

// Tool 1: Build system
const buildCache = await findCacheDir("project-tools");

// Tool 2: Test runner (shares same cache)
const testCache = await findCacheDir("project-tools");

console.log(buildCache === testCache); // true

Next Steps

Browser and Server Support

This package is designed for Node.js environments:

  • Node.js: ≥22.13 ≤25.x
  • Browsers: Not applicable (filesystem operations)
  • Deno/Bun: Compatible with filesystem APIs
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues