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.
default
✓ import LRU from 'lru-cache-ext'
✗ const LRU = require('lru-cache-ext')
Package is ESM-only for Node >=20.
memoize
✓ import { memoize } from 'lru-cache-ext'
✗ import memoize from 'lru-cache-ext'
memoize is a named export, not default.
memoizeSync
✓ import { memoizeSync } from 'lru-cache-ext'
Named export for synchronous memoization.
Demonstrates creating a cache instance, async memoize, sync memoize, and cacheNull option.
import LRU, { memoize, memoizeSync } from 'lru-cache-ext';
const cache = new LRU({ max: 100, maxAge: 1000 * 60 * 5 });
// memoize async example
function fetchData(id) {
return new Promise(resolve => {
setTimeout(() => resolve(`Data for ${id}`), 100);
});
}
async function test() {
const result1 = await memoize('key1', () => fetchData(1));
console.log(result1); // 'Data for 1'
const result2 = await memoize('key1', () => fetchData(2)); // returns cached
console.log(result2); // 'Data for 1'
}
test();
// memoizeSync example
function expensiveSync(key) {
return key.toUpperCase();
}
const syncResult = memoizeSync('syncKey', () => expensiveSync('hello'));
console.log(syncResult); // 'HELLO'
// cacheNull option
const cacheNoNull = new LRU({ max: 10, cacheNull: false });
memoize('nullKey', () => null).then(val => console.log(val)); // null is not cached
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/lru-cache-ext/index.js from ... not supported.
Using CommonJS require() with an ESM-only package.
fixSwitch to ES module import syntax or use dynamic import().
TypeError: LRU is not a constructor
Using default import as a constructor but the default export is the extended LRU class? Actually the default export is the class itself. This error could occur if you imported the wrong thing.
fixEnsure you are using: import LRU from 'lru-cache-ext'
Audit
Dependencies
lru-cacherequiredCore caching functionality; lru-cache-ext wraps it.