Registry / database / least-recent

least-recent

JSON →
library1.0.3jsnpmunverified

An LRU cache that evicts least-recently-used items when full. Current version 1.0.3, stable with low release cadence. Fork of Mnemonist's LRUCache with added eviction event emission via nanoevents. Differentiator: emits 'evicted' event on eviction, unlike Mnemonist. Pure ESM package, requires Node >=14 or modern browser.

npm install least-recent
INSTALL
IMPORT
SIG · LEAST-RECENT
L
least-recent
databasejavascriptv1.0.3
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.

LRUCache
import { LRUCache } from 'least-recent'
const LRUCache = require('least-recent')
ESM-only since v1.0.0; no CommonJS support.
LRUCache
import { LRUCache } from 'least-recent'
import LRUCache from 'least-recent'
Only named export, no default export.

Creates an LRU cache with capacity 3, adds entries, demonstrates get and eviction event listener.

import { LRUCache } from 'least-recent'; const cache = new LRUCache<string, number>(3); cache.set('a', 1); cache.set('b', 2); console.log(cache.get('a')); // 1 cache.set('c', 3); console.log(cache.get('b')); // 2 cache.set('d', 4); // 'c' was evicted console.log('size:', cache.size); // 3 cache.on('evicted', (key, value) => { console.log(`evicted: ${key}=${value}`); });
Debug
Known issues
breakingPackage is ESM-only; requires Node >=14 and "type": "module" in package.json.
fix
Ensure your project is ESM or use dynamic import() for CommonJS.
affects: >=1.0.0
deprecatedThe 'evicted' event may be removed in future major versions; prefer subclassing if event logic is critical.
fix
Monitor releases; consider forking if event behavior is essential.
affects: >=1.0.0
gotchaCapacity is required; no default. If omitted, cache does not evict and grows unbounded.
fix
Always pass a positive integer capacity.
affects: >=1.0.0
gotchaThe cache emits 'evicted' only on set() when capacity is exceeded, not on other methods like delete() or clear().
fix
Handle eviction only once per set() and not on explicit removal.
affects: >=1.0.0
gotchaMeant for in-memory caching in Node.js; may not work in older browsers without polyfills for Map and WeakMap.
fix
Use a transpiler/bundler that provides Map polyfill if targeting very old environments.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token 'export'
CommonJS require() of ESM package.
fix
Use import { LRUCache } from 'least-recent' instead of require.
Error [ERR_REQUIRE_ESM]: require() of ES Module
Running CommonJS script trying to require the package.
fix
Convert to ES module ("type": "module" in package.json) or use dynamic import()
TypeError: cache.on is not a function
Attempting to use 'on' method on an older version without eviction events or misunderstanding API.
fix
Ensure you installed version 1.0.0+ and are calling on('evicted', callback).
RangeError: Invalid capacity: must be a positive integer
Passing a non-positive or non-integer capacity to LRUCache constructor.
fix
Pass a positive integer (e.g., new LRUCache(1000)).
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
nanoeventsrequiredEvent emitter for the 'evicted' event; pulled in automatically.
Agent activity
6 hits · last 30 days
node
4
Resources
least-recent — npm install least-recent · libregistry