Find Cache DirUsage Guide

Usage Guide

Learn how to use @visulima/find-cache-dir

Last updated:

Usage Guide

Learn how to find and use cache directories in your applications.

Basic Usage

Async Version

Use the async version in modern async/await code:

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

Sync Version

Use the sync version when you need immediate results:

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

const cacheDir = findCacheDirSync("my-app");
console.log(cacheDir);

Options

Create Directory Automatically

Create the cache directory if it doesn't exist:

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

const cacheDir = await findCacheDir("my-app", { create: true });
// Directory is created if it doesn't exist

Custom Starting Directory

Specify where to start searching for package.json:

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

const cacheDir = await findCacheDir("my-app", {
  cwd: "/path/to/project"
});

Error Handling

Control error throwing behavior:

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

try {
  const cacheDir = await findCacheDir("my-app", {
    throwError: true
  });
} catch (error) {
  console.error("Cache directory not found:", error);
}

Without throwError, returns undefined if not found:

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

const cacheDir = await findCacheDir("my-app");
if (cacheDir) {
  console.log("Found:", cacheDir);
} else {
  console.log("No cache directory found");
}

Real-World Examples

Cache Compiled Code

Store compilation results for faster rebuilds:

import { findCacheDir } from "@visulima/find-cache-dir";
import { writeFileSync, readFileSync, existsSync } from "fs";
import { join } from "path";
import { createHash } from "crypto";

async function compileWithCache(sourceFile: string): Promise<string> {
  const cacheDir = await findCacheDir("my-compiler", { create: true });
  const source = readFileSync(sourceFile, "utf-8");
  const hash = createHash("md5").update(source).digest("hex");
  const cachePath = join(cacheDir, `${hash}.js`);

  if (existsSync(cachePath)) {
    console.log("Using cached compilation");
    return readFileSync(cachePath, "utf-8");
  }

  console.log("Compiling...");
  const compiled = await compile(source);

  writeFileSync(cachePath, compiled);
  return compiled;
}

Package Manager Cache

Implement a package cache system:

import { findCacheDir } from "@visulima/find-cache-dir";
import { join } from "path";
import { createWriteStream, createReadStream } from "fs";
import { pipeline } from "stream/promises";

class PackageCache {
  private cacheDir: string | undefined;

  async init() {
    this.cacheDir = await findCacheDir("my-package-manager", {
      create: true
    });
  }

  async cachePackage(name: string, version: string, stream: NodeJS.ReadableStream) {
    if (!this.cacheDir) throw new Error("Cache not initialized");

    const cachePath = join(this.cacheDir, `${name}@${version}.tgz`);
    await pipeline(stream, createWriteStream(cachePath));
  }

  getCachedPackage(name: string, version: string): NodeJS.ReadableStream | null {
    if (!this.cacheDir) return null;

    const cachePath = join(this.cacheDir, `${name}@${version}.tgz`);
    try {
      return createReadStream(cachePath);
    } catch {
      return null;
    }
  }
}

Development Server Cache

Cache development server state:

import { findCacheDirSync } from "@visulima/find-cache-dir";
import { writeFileSync, readFileSync, existsSync } from "fs";
import { join } from "path";

class DevServerState {
  private stateFile: string;

  constructor(projectName: string) {
    const cacheDir = findCacheDirSync(projectName, { create: true });
    if (!cacheDir) throw new Error("Could not create cache directory");
    this.stateFile = join(cacheDir, "dev-server-state.json");
  }

  save(state: Record<string, unknown>) {
    writeFileSync(this.stateFile, JSON.stringify(state, null, 2));
  }

  load(): Record<string, unknown> | null {
    if (!existsSync(this.stateFile)) return null;
    return JSON.parse(readFileSync(this.stateFile, "utf-8"));
  }
}

// Usage
const state = new DevServerState("my-dev-server");
state.save({ port: 3000, lastUsed: Date.now() });

const loaded = state.load();
console.log("Previous state:", loaded);

Environment Variables

Override Cache Directory

For testing, override with CACHE_DIR environment variable:

CACHE_DIR=/tmp/test-cache node my-script.js
import { findCacheDir } from "@visulima/find-cache-dir";

// Will use /tmp/test-cache if CACHE_DIR is set
const cacheDir = await findCacheDir("my-app");

Platform-Specific Behavior

macOS

Cache stored in ~/Library/Caches/:

/Users/username/Library/Caches/my-app/

Linux

Cache stored in ~/.cache/ or $XDG_CACHE_HOME:

/home/username/.cache/my-app/

Windows

Cache stored in %LOCALAPPDATA%:

C:\Users\username\AppData\Local\my-app\

Best Practices

Name Consistency

Use your package name from package.json:

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

const pkg = JSON.parse(readFileSync("package.json", "utf-8"));
const cacheDir = await findCacheDir(pkg.name, { create: true });

Error Handling

Always handle potential undefined results:

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

const cacheDir = await findCacheDir("my-app");

if (!cacheDir) {
  console.warn("No cache directory available, using temp directory");
  // Fallback logic
}

Cleanup Old Cache

Implement cache cleanup strategies:

import { findCacheDir } from "@visulima/find-cache-dir";
import { readdirSync, statSync, unlinkSync } from "fs";
import { join } from "path";

async function cleanOldCache(maxAgeDays: number = 30) {
  const cacheDir = await findCacheDir("my-app");
  if (!cacheDir) return;

  const now = Date.now();
  const maxAge = maxAgeDays * 24 * 60 * 60 * 1000;

  for (const file of readdirSync(cacheDir)) {
    const filePath = join(cacheDir, file);
    const stats = statSync(filePath);

    if (now - stats.mtimeMs > maxAge) {
      unlinkSync(filePath);
      console.log(`Removed old cache file: ${file}`);
    }
  }
}

Next Steps

API Reference

Complete API documentation

Back to Overview

Return to package overview

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