Registry / testing / node-storage-shim

node-storage-shim

JSON →
library2.0.1jsnpmunverified

An in-memory implementation of the W3C Storage Interface (localStorage/sessionStorage) for use as a shim in Node.js environments, primarily for unit testing. v2.0.1 (March 2020) is the latest stable release. It uses Proxy (since v2.0.0) to allow direct property access while maintaining the storage API. It does not persist data to disk and does not implement the 'storage' event. Lightweight, TypeScript declarations included.

npm install node-storage-shim
INSTALL
IMPORT
SIG · NODE-STORAGE-SHIM
N
node-storage-shim
testingjavascriptv2.0.1
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.

StorageShim
import StorageShim from 'node-storage-shim'
const { StorageShim } = require('node-storage-shim')
Default export; named import destructuring is incorrect. CommonJS require should be const StorageShim = require('node-storage-shim')
StorageShim
const StorageShim = require('node-storage-shim')
const { StorageShim } = require('node-storage-shim')
Correct CommonJS pattern; named destructuring is wrong.
StorageShim
import type StorageShim from 'node-storage-shim'
import { StorageShim } from 'node-storage-shim'
TypeScript: use default type import for type-only usage; named import is incorrect.

Shows creation of a StorageShim instance, basic set/get/remove operations, and polyfilling localStorage/sessionStorage in Node.js.

// Node.js v6.14.4+ with CommonJS const StorageShim = require('node-storage-shim'); const storage = new StorageShim(); storage.setItem('key', 'value'); console.log(storage.getItem('key')); // 'value' console.log(storage.length); // 1 storage.removeItem('key'); console.log(storage.length); // 0 // Polyfill globals (for Node.js) global.localStorage = new StorageShim(); global.sessionStorage = new StorageShim();
Debug
Known issues
breakingVersion 2.0.0 dropped support for Node < 6.14.4 and introduced Proxy for direct property access, which may break code that relied on enumerable keys or the old storage behavior.
fix
Upgrade Node to >=6.14.4 and test code that iterates over storage keys or directly accesses properties.
affects: >=2.0.0
deprecatedMethod names (e.g., 'setItem', 'getItem') cannot be used as keys due to internal implementation using Proxy. This is a known limitation.
fix
Avoid using keys that conflict with Storage interface method names.
affects: >=2.0.0
gotchaDoes not implement the 'storage' event. If your code relies on storage event listeners, this shim will not trigger them.
fix
Manually mock the storage event or use a different shim that supports it.
affects: >=1.0.0
gotchaData is not persisted to disk. It is solely in-memory and will be lost on process exit. This is by design for testing.
fix
Do not use for production persistence; use a real localStorage/sessionStorage or a persistent store instead.
affects: >=1.0.0
gotchaTypeScript declaration is provided, but the package does not ship a separate .d.ts file; it is bundled via 'types' field in package.json.
fix
Ensure tsconfig includes node_modules types. Import using default import syntax.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'node-storage-shim'
Package not installed or import path typo.
fix
Run `npm install node-storage-shim --save-dev` or check the import statement.
TypeError: StorageShim is not a constructor
Incorrect import: using named destructuring when default export is expected.
fix
Use `const StorageShim = require('node-storage-shim')` in CommonJS or `import StorageShim from 'node-storage-shim'` in ESM.
TypeError: storage.getItem is not a function
StorageShim constructor was called without 'new' or the instance was overwritten.
fix
Always use `new StorageShim()` to create an instance.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
node-storage-shim — npm install node-storage-shim · libregistry