README.md
1# lazy-cache [](https://www.npmjs.com/package/lazy-cache) [](https://npmjs.org/package/lazy-cache) [](https://travis-ci.org/jonschlinkert/lazy-cache)
2
3> Cache requires to be lazy-loaded when needed.
4
5## Install
6
7Install with [npm](https://www.npmjs.com/):
8
9```sh
10$ npm install lazy-cache --save
11```
12
13If you use webpack and are experiencing issues, try using [unlazy-loader](https://github.com/doowb/unlazy-loader), a webpack loader that fixes the bug that prevents webpack from working with native javascript getters.
14
15## Usage
16
17```js
18var utils = require('lazy-cache')(require);
19```
20
21**Use as a property on `lazy`**
22
23The module is also added as a property to the `lazy` function
24so it can be called without having to call a function first.
25
26```js
27var utils = require('lazy-cache')(require);
28
29// `npm install glob`
30utils('glob');
31
32// glob sync
33console.log(utils.glob.sync('*.js'));
34
35// glob async
36utils.glob('*.js', function (err, files) {
37 console.log(files);
38});
39```
40
41**Use as a function**
42
43```js
44var utils = require('lazy-cache')(require);
45var glob = utils('glob');
46
47// `glob` is a now a function that may be called when needed
48glob().sync('foo/*.js');
49```
50
51## Aliases
52
53An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
54
55**Example**
56
57```js
58var utils = require('lazy-cache')(require);
59
60// alias `ansi-yellow` as `yellow`
61utils('ansi-yellow', 'yellow');
62console.log(utils.yellow('foo'));
63```
64
65## Browserify usage
66
67**Example**
68
69```js
70var utils = require('lazy-cache')(require);
71// temporarily re-assign `require` to trick browserify
72var fn = require;
73require = utils;
74// list module dependencies (here, `require` is actually `lazy-cache`)
75require('glob');
76require = fn; // restore the native `require` function
77
78/**
79 * Now you can use glob with the `utils.glob` variable
80 */
81
82// sync
83console.log(utils.glob.sync('*.js'));
84
85// async
86utils.glob('*.js', function (err, files) {
87 console.log(files.join('\n'));
88});
89```
90
91## Kill switch
92
93In certain rare edge cases it may be necessary to unlazy all lazy-cached dependencies (5 reported cases after ~30 million downloads).
94
95To force lazy-cache to immediately invoke all dependencies, do:
96
97```js
98process.env.UNLAZY = true;
99```
100
101## Related projects
102
103You might also be interested in these projects:
104
105[lint-deps](https://www.npmjs.com/package/lint-deps): CLI tool that tells you when dependencies are missing from package.json and offers you a… [more](https://www.npmjs.com/package/lint-deps) | [homepage](https://github.com/jonschlinkert/lint-deps)
106
107## Contributing
108
109Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/lazy-cache/issues/new).
110
111## Building docs
112
113Generate readme and API documentation with [verb](https://github.com/verbose/verb):
114
115```sh
116$ npm install verb && npm run docs
117```
118
119Or, if [verb](https://github.com/verbose/verb) is installed globally:
120
121```sh
122$ verb
123```
124
125## Running tests
126
127Install dev dependencies:
128
129```sh
130$ npm install -d && npm test
131```
132
133## Author
134
135**Jon Schlinkert**
136
137* [github/jonschlinkert](https://github.com/jonschlinkert)
138* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
139
140## License
141
142Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
143Released under the [MIT license](https://github.com/jonschlinkert/lazy-cache/blob/master/LICENSE).
144
145***
146
147_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 22, 2016._