Registry / devops / lazy-cache

lazy-cache

JSON →
library2.0.2jsnpmunverified

lazy-cache v2.0.2 is a tiny utility that defers requiring npm modules until they are actually accessed. It uses JavaScript getters to lazily load dependencies, which can reduce startup time and avoid executing problematic code from dependencies (e.g., libraries that modify globals or run side effects on require). The package is stable but effectively in maintenance mode (last release 2018). Key differentiators: no magic, native getters; works with CommonJS; has a separate webpack loader for bundler compatibility. However, it is largely superseded by ESM dynamic imports and tree-shaking.

npm install lazy-cache
INSTALL
IMPORT
SIG · LAZY-CACHE
L
lazy-cache
devopsjavascriptv2.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

lazy-cache (the function)
const lazy = require('lazy-cache')(require);
import lazy from 'lazy-cache';
The package only provides a CommonJS function that must be called with `require` as argument. ESM import will fail because it has no default export.
utils (the lazy module holder)
const utils = require('lazy-cache')(require); utils('glob'); utils.glob();
const { glob } = require('lazy-cache')(require);
The returned object is used as both a function and a namespace. Destructuring directly from the lazy call is unreliable.
using a lazy-loaded module
const utils = require('lazy-cache')(require); utils('lodash'); utils.lodash.chunk([1,2,3], 2);
const lodash = require('lazy-cache')('lodash');
The lazy function must be called on the result of `require('lazy-cache')(require)`, not directly on the package itself.

Shows how to require the lazy-cache function, register modules, and use them lazily with both property access and aliased function style.

// Install: npm install lazy-cache const lazy = require('lazy-cache')(require); lazy('glob'); // registers lazy loader for 'glob' lazy('micromatch', 'mm'); // alias example // Use when needed lazy.glob('*.js', (err, files) => { console.log(files); }); // Or as function const glob = lazy('glob'); glob().sync('*.js');
Debug
Known issues
breakinglazy-cache does not work with webpack without a loader due to webpack's static analysis
fix
Use the unlazy-loader (https://github.com/doowb/unlazy-loader) as a webpack plugin.
affects: >=0.0.1
gotchaThe package only supports CommonJS (require) and has no ESM module export. Using 'import lazy from "lazy-cache"' will throw an error.
fix
Use require() or migrate to ESM dynamic import() which serves a similar purpose natively.
affects: >=2.0.0
deprecatedlazy-cache is no longer actively maintained; the last release 2.0.2 was in 2018.
fix
Consider using ES2020 dynamic import() for modern projects, or a more maintained lazy-loading library.
affects: >=2.0.0
gotchaThe lazy function must be called with `require` as the only argument. Passing a different resolver may cause unexpected behavior.
fix
Always call as `require('lazy-cache')(require)`.
affects: >=0.0.1
Errors
Common errors & fixes
Cannot find module 'lazy-cache'
The package is not installed or the import path is incorrect.
fix
Run `npm install lazy-cache` and ensure you are using `require('lazy-cache')` (CommonJS).
TypeError: lazy is not a function
Incorrectly using ESM import or calling the package without (require).
fix
Use `const lazy = require('lazy-cache')(require);`
Module not found: Error: Can't resolve 'lazy-cache' in ...
Webpack cannot resolve the module because it is CommonJS-only and webpack may need configuration.
fix
Add the unlazy-loader to webpack config, or avoid using lazy-cache with webpack.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
lazy-cache — npm install lazy-cache · libregistry