UNPKG

2.36 kBMarkdownView Raw
1# find-cache-dir
2
3> Finds the common standard cache directory
4
5The [`nyc`](https://github.com/istanbuljs/nyc) and [`AVA`](https://ava.li) projects decided to standardize on a common directory structure for storing cache information:
6
7```sh
8# nyc
9./node_modules/.cache/nyc
10
11# ava
12./node_modules/.cache/ava
13
14# your-module
15./node_modules/.cache/your-module
16```
17
18This 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:
19
20```
21rm -rf ./node_modules/.cache
22```
23
24## Install
25
26```sh
27npm install find-cache-dir
28```
29
30## Usage
31
32```js
33import findCacheDirectory from 'find-cache-dir';
34
35findCacheDirectory({name: 'unicorns'});
36//=> '/user/path/node-modules/.cache/unicorns'
37```
38
39## API
40
41### findCacheDirectory(options?)
42
43Finds the cache directory using the given options.
44
45The 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.
46
47#### options
48
49Type: `object`
50
51##### name
52
53*Required*\
54Type: `string`
55
56Should be the same as your project name in `package.json`.
57
58##### files
59
60Type: `string[]`
61
62An 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.
63
64##### cwd
65
66Type: `string`\
67Default `process.cwd()`
68
69The directory to start searching for a `package.json` from.
70
71##### create
72
73Type: `boolean`\
74Default `false`
75
76Create the directory synchronously before returning.
77
78## Tips
79
80- To test modules using `find-cache-dir`, set the `CACHE_DIR` environment variable to temporarily override the directory that is resolved.
81
82## Adopters
83
84- [`ava`](https://avajs.dev)
85- [`nyc`](https://github.com/istanbuljs/nyc)
86- [`storybook`](https://github.com/storybookjs/storybook)
87- [`babel-loader`](https://github.com/babel/babel-loader)
88- [`eslint-loader`](https://github.com/MoOx/eslint-loader)
89- [More…](https://www.npmjs.com/browse/depended/find-cache-dir)