Registry / storage / simple-memory-storage

simple-memory-storage

JSON →
library1.0.0jsnpmunverified

A lightweight, zero-dependency in-memory storage implementation that mirrors the Web Storage API (localStorage/sessionStorage). Version 1.0.0 is the initial stable release, with no further updates since. It provides a simple JavaScript object-based store with methods: setItem, getItem, removeItem, clear, key, and length. Differentiates from localStorage by not persisting data, making it ideal for testing and ephemeral state in Node.js or browser environments. Includes TypeScript types.

npm install simple-memory-storage
INSTALL
IMPORT
SIG · SIMPLE-MEMORY-STOR
S
simple-memory-storage
storagejavascriptv1.0.0
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.

SimpleMemoryStorage
import { SimpleMemoryStorage } from 'simple-memory-storage'
import SimpleMemoryStorage from 'simple-memory-storage'
Only named export available; default import will cause runtime error.
SimpleMemoryStorage
import { SimpleMemoryStorage } from 'simple-memory-storage'
const { SimpleMemoryStorage } = require('simple-memory-storage')
Package is ESM-only; CommonJS require is not supported.
SimpleMemoryStorage
const { SimpleMemoryStorage } = await import('simple-memory-storage')
const SimpleMemoryStorage = require('simple-memory-storage')
Dynamic import works in ESM; require will throw MODULE_NOT_FOUND.

Instantiate SimpleMemoryStorage and use Web Storage API methods (setItem, getItem, removeItem, clear, key, length).

import { SimpleMemoryStorage } from 'simple-memory-storage'; const storage = new SimpleMemoryStorage(); storage.setItem('key1', 'value1'); console.log(storage.length); // 1 console.log(storage.key(0)); // 'key1' console.log(storage.getItem('key1')); // 'value1' storage.setItem('key2', 'value2'); console.log(storage.length); // 2 console.log(storage.getItem('key2')); // 'value2' storage.removeItem('key2'); console.log(storage.getItem('key2')); // null storage.clear(); console.log(storage.length); // 0
Debug
Known issues
gotchaStorage is not shared between instances; each SimpleMemoryStorage has its own independent data.
fix
Create a single instance and pass it around, or use a global singleton pattern.
affects: >=1.0.0
gotchaThe length property is a number that only updates after mutations; it is not reactive.
fix
Always read length after calling setItem/removeItem/clear to get the current count.
affects: >=1.0.0
gotchakey() returns null if index is out of bounds; does not throw an error.
fix
Check if the returned value is null before using it as a key.
affects: >=1.0.0
gotchaThe storage only stores string values; other types are coerced to strings.
fix
Use JSON.stringify/parse for non-string data.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: simple_memory_storage_1.SimpleMemoryStorage is not a constructor
Using default import instead of named import.
fix
Change to `import { SimpleMemoryStorage } from 'simple-memory-storage'`.
Cannot find module 'simple-memory-storage'
Attempting to use CommonJS require on an ESM-only package.
fix
Switch to ESM imports (`import { SimpleMemoryStorage } from 'simple-memory-storage'`) or use dynamic import in CommonJS.
TypeError: storage.key is not a function
Using a numeric index directly on the storage object instead of the key() method.
fix
Use `storage.key(index)` instead of `storage[index]`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
33 hits · last 30 days
node
32
Resources
simple-memory-storage — npm install simple-memory-storage · libregistry