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.
memoizer
✓ import memoizer from 'lru-memoizer'
✗ const memoizer = require('lru-memoizer');
Package supports both ESM and CJS. Default import works for both.
memoizer.sync
✓ import memoizer from 'lru-memoizer'; const { sync } = memoizer;
✗ import { memoizer } from 'lru-memoizer';
memoizer is default export. sync is a property on the default export.
MemoizeOptions
✓ import type { MemoizeOptions } from 'lru-memoizer'
✗ none
Type import for TypeScript. No wrong pattern known.
Creates a memoized async function that fetches JSON data, caching results for 5 minutes with a max of 100 entries.
import memoizer from 'lru-memoizer';
import got from 'got';
const memoizedRequest = memoizer({
load: (url, callback) => {
got(url).json().then(data => callback(null, data)).catch(callback);
},
hash: (url) => url,
max: 100,
ttl: 1000 * 60 * 5,
});
memoizedRequest('https://api.example.com/data', (err, data) => {
if (err) console.error(err);
else console.log(data);
});
Debug
Known issues
breakingv3.0 uses lru-cache v7 which changed constructor options (e.g., 'maxAge' replaced by 'ttl').fixUse 'ttl' instead of 'maxAge' when upgrading from v2 to v3.
affects: <3.0.0 to >=3.0.0
gotchaResults are deep-frozen or deep-cloned only if 'freeze' or 'clone' options are enabled; otherwise mutations affect cached value.fixSet 'clone: true' if you need to mutate returned objects safely.
affects: >=0.0.0
gotchaThe 'hash' function must return a string synchronously. If it returns undefined, caching may not work as expected.fixEnsure your hash function always returns a string (e.g., return String(key)).
affects: >=0.0.0
deprecatedThe 'length' option from lru-cache is deprecated in favor of 'sizeCalculation' in lru-cache v7+.fixUse 'sizeCalculation' instead of 'length' in lru-memoizer options (passed through to lru-cache).
affects: >=3.0.0
Errors
Common errors & fixes
Error: The 'hash' function should return a string.
hash function returned non-string or is missing.
fixEnsure hash returns a string; e.g., hash: (opts) => opts.url + JSON.stringify(opts.qs)
TypeError: callback is not a function
The 'load' function's last parameter must be a callback, but you may have omitted it or called with wrong signature.
fixDefine load with correct signature: load: function(...args, callback) { ... } Cannot find module 'lru-memoizer'
Package not installed or import path incorrect.
fixRun 'npm install lru-memoizer' and ensure correct import (default import from 'lru-memoizer').
Audit
Dependencies
lru-cacherequiredUnderlying LRU cache implementation