Back to directory
sindresorhus avatar

find-cache-directory

Finds the common standard cache directory. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/sindresorhus/find-cache-directory.git

163

Stars

24

Forks

1

Watchers

MIT

License

find-cache-directory

Finds the common standard cache directory

The nyc and AVA projects decided to standardize on a common directory structure for storing cache information:

# nyc
./node_modules/.cache/nyc

# ava
./node_modules/.cache/ava

# your-module
./node_modules/.cache/your-module

This module makes it easy to correctly locate the cache directory according to this shared spec. If this pattern becomes ubiquitous, clearing the cache for multiple dependencies becomes easy and consistent:

rm -rf ./node_modules/.cache

Install

npm install find-cache-directory

Usage

import findCacheDirectory from 'find-cache-directory';

findCacheDirectory({name: 'unicorns'});
//=> '/user/path/node-modules/.cache/unicorns'

API

findCacheDirectory(options?)

Finds the cache directory using the given options.

The algorithm checks for the CACHE_DIR environmental variable and uses it if it is not set to true, false, 1 or 0. If one is not found, it tries to find a package.json file, searching every parent directory of the cwd specified (or implied from other options). It returns a string containing the absolute path to the cache directory, or undefined if package.json was never found or if the node_modules directory is unwritable.

options

Type: object

name

Required
Type: string

Should be the same as your project name in package.json.

files

Type: string[]

An array of files that will be searched for a common parent directory. This common parent directory will be used in lieu of the cwd option below.

cwd

Type: string
Default process.cwd()

The directory to start searching for a package.json from.

create

Type: boolean
Default false

Create the directory synchronously before returning.

Tips

  • To test modules using this package, set the CACHE_DIR environment variable to temporarily override the directory that is resolved.

Adopters

  • env-paths - Get paths for storing things like data, config, cache, etc

Releases

Breaking

Require Node.js 20 8e826b0 Rename package from find-cache-dir to find-cache-directory 81a1286

v5.0.0...v6.0.0

Aug 18, 2023

Download .zip

Breaking

Require Node.js 16 e14dff6 Remove thunk option e14dff6

Just use path.join() on the result.

The files option now only accepts an array of strings e14dff6

Previously, it accepted both a...

Breaking

Require Node.js 14 c5e8e5c This package is now pure ESM. Please read this.

Improvements

Upgrade dependencies Reduce amount of dependencies

v3.3.2...v4.0.0

Aug 29, 2021

Download .zip

Respect name option when CACHE_DIR environment variable is set (#34) 6a378f5

v3.3.1...v3.3.2

Ignore true/false/1/0 values for CACHE_DIR environment variable (#27) e7dd4a6

v3.3.0...v3.3.1