Registry / storage / node-localcache

node-localcache

JSON →
library0.1.4jsnpmunverified

A simple Node.js module that mimics the LocalStorage API, persisting data to a JSON file between application launches. Version 0.1.4 is the latest stable release. The library aggregates write operations and flushes to disk once per second, ensuring data is saved on process exit. It supports optional in-memory-only mode (without file caching). Differentiator: lightweight, no external dependencies, and familiar API for developers coming from browser LocalStorage.

npm install node-localcache
INSTALL
IMPORT
SIG · NODE-LOCALCACHE
N
node-localcache
storagejavascriptv0.1.4
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.

LocalCache
const LocalCache = require('node-localcache');
import LocalCache from 'node-localcache';
This package does not provide an ESM default export. Use CommonJS require.
new LocalCache(fileName, skipFileCaching)
const cache = new LocalCache('.cache/data.json');
const cache = new LocalCache();
If skipFileCaching is false (default), fileName is required. Omitting it will cause an error.
setItem(key, value)
cache.setItem('myKey', { foo: 'bar' });
cache.setItem('myKey', 'value', 1000);
No expiration or TTL support. Only key-value pairs are stored.
getItem(key)
const val = cache.getItem('myKey');
const val = cache.getItem('myKey', 'default');
No default value parameter. Returns undefined if key not found.

Demonstrates basic usage: creating a cache instance backed by a JSON file, setting, getting, and removing items.

const LocalCache = require('node-localcache'); const path = require('path'); const cacheFile = path.join(__dirname, 'cache.json'); const cache = new LocalCache(cacheFile); cache.setItem('username', 'alice'); console.log(cache.getItem('username')); // 'alice' cache.removeItem('username'); console.log(cache.getItem('username')); // undefined
Debug
Known issues
breakingThe constructor throws if fileName is not provided when skipFileCaching is false (default).
fix
Always provide a file path or explicitly set skipFileCaching: true for in-memory only.
affects: >=0.1.0
deprecatedThe library relies on synchronous file operations during flush (setInterval 1s).
fix
Avoid using in high-concurrency production environments; consider alternatives like node-persist or lowdb.
affects: >=0.1.0
gotchaData is written to file in one-second intervals. If the process crashes within that window, recent changes may be lost.
fix
Use graceful shutdown or call a hypothetical flush method (not provided). Mitigate by reducing interval or using synchronous writes.
affects: >=0.1.0
gotchaThe package does not export anything else besides the constructor. Trying to import like an ES module will fail.
fix
Use CommonJS require() as shown in the README.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read property 'setItem' of undefined
Cache instance not created correctly (missing new keyword).
fix
const cache = new LocalCache('file.json');
Error: fileName must be a string or skipFileCaching must be true
Constructor called without arguments and skipFileCaching defaults to false.
fix
Provide a file path: new LocalCache('./cache.json');
Uncaught ReferenceError: require is not defined
Using the package in a browser environment or ESM context without bundler support.
fix
Use a bundler like webpack, or import via script tag with a CommonJS-compatible loader.
Upgrade
Version history
0.1.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
22
Amazon
1
Resources
node-localcache — npm install node-localcache · libregistry