Registry / testing / dom-storage

dom-storage

JSON →
library2.1.0jsnpmunverified

A W3C-compliant DOM Storage (localStorage and sessionStorage) implementation for Node.js, allowing browser-oriented unit tests to run server-side. Version 2.1.0 is stable with no recent releases. It supports in-memory and file-backed storage with configurable strict stringification and JSON whitespace. Unlike alternatives like node-localstorage, this is synchronous and does not emit Storage events. Ideal for simple test mocking, not production use.

npm install dom-storage
INSTALL
IMPORT
SIG · DOM-STORAGE
D
dom-storage
testingjavascriptv2.1.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.

Storage
const Storage = require('dom-storage');
import Storage from 'dom-storage';
Package is CommonJS-only; ESM import not supported
instance methods
const store = new Storage(null, { strict: true }); store.setItem('key', 'val');
const store = new Storage(); store.setItem('key', 1); // strict:true converts to string
Use strict: true for W3C stringification; default is JSON-like
constructor options
const store = new Storage('./db.json', { strict: false, ws: ' ' });
const store = new Storage('./db.json'); // missing options object
ws option only meaningful when file path given

Demonstrates in-memory and file-backed storage, strict vs non-strict mode, and JSON serialization usage.

const Storage = require('dom-storage'); const localStorage = new Storage(null, { strict: true }); localStorage.setItem('name', 'Alice'); console.log(localStorage.getItem('name')); // 'Alice' console.log(localStorage.length); // 1 console.log(localStorage.key(0)); // 'name' localStorage.removeItem('name'); console.log(localStorage.length); // 0 localStorage.setItem('obj', JSON.stringify({ a: 1 })); console.log(JSON.parse(localStorage.getItem('obj'))); // { a: 1 } const fileStorage = new Storage('./db.json', { strict: false, ws: ' ' }); fileStorage.setItem('x', { y: 2 }); console.log(fileStorage.getItem('x')); // { y: 2 } (object, not string)
Debug
Known issues
gotchaDefault strict mode is false; values are stored as-is (objects remain objects), not stringified as per W3C.
fix
Pass { strict: true } to conform to W3C spec (all values as strings).
affects: >=0.0.0
gotchaFile-backed storage is synchronous and reads entire DB on construction; no save callback.
fix
Avoid for large files or high-frequency writes; consider async alternatives.
affects: >=0.0.0
gotchaNo Storage events (e.g., 'storage') are emitted; cannot listen for changes from other tabs/processes.
fix
Use a polyfill or different library if events are needed.
affects: >=0.0.0
deprecatedPackage has not been updated since 2018; may have unpatched issues.
fix
Evaluate alternatives like node-localstorage or simple-memory-storage.
affects: >=2.1.0
Errors
Common errors & fixes
TypeError: dom_storage_1.default is not a constructor
Using ES import syntax on a CommonJS module
fix
Use require('dom-storage') instead of import.
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: '1' === 1
strict mode converts numbers to strings, but test expects number
fix
Use strict: true and compare strings, or use non-strict mode and compare values as stored.
Error: ENOENT: no such file or directory, open './nonexistent/db.json'
File path directory does not exist; dom-storage does not create directories
fix
Ensure directory exists before constructing with file path.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
dom-storage — npm install dom-storage · libregistry