Registry / storage / json-storage

json-storage

JSON →
library2.1.2jsnpmunverified

A lightweight wrapper for W3C Storage API (localStorage, sessionStorage, globalStorage) with JSON serialization, namespace support, and optional stringify control. Version 2.1.2 provides a stable API for browser and Node.js environments. Key differentiators: simple abstraction with namespace isolation, compatibility with various DOMStorage implementations, and configurable stringification to avoid double encoding. The library has low activity and known gotchas around undefined/null handling.

npm install json-storage
INSTALL
IMPORT
SIG · JSON-STORAGE
J
json-storage
storagejavascriptv2.1.2
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.

JsonStorage
const { JsonStorage } = require('json-storage')
const JsonStorage = require('json-storage')
Named export in CommonJS
JsonStorage
import { JsonStorage } from 'json-storage'
import JsonStorage from 'json-storage'
Named export, not default. Requires Node >= v0.2.0
create
const store = JsonStorage.create(localStorage, 'ns', opts)
const store = JsonStorage.create(window.localStorage, 'ns')
In browser, can omit first arg (defaults to window.localStorage) or pass null. In Node, must provide localStorage module.

Shows basic usage with Node.js: create a namespaced store, set/get/remove JSON values, and avoid double stringification.

const localStorage = require('localStorage'); const { JsonStorage } = require('json-storage'); const store = JsonStorage.create(localStorage, 'my-app', { stringify: false }); store.set('user', { name: 'Alice' }); const user = store.get('user'); console.log(user); // { name: 'Alice' } store.remove('user'); console.log(store.get('user')); // null console.log(store.keys()); // []
Debug
Known issues
gotcha`store.set('key', undefined)` removes the key (same as `store.remove('key')`).
fix
Use `null` instead of `undefined` to represent 'no value'.
affects: *
gotchaBoth missing keys and keys set to `null` return `null` from `store.get()`. You cannot distinguish between them.
fix
Use `store.has(key)` or `store.keys()` to check existence if needed.
affects: *
gotchaThe string `"null"` (the literal four characters) is stored as the JSON value `null` when stored alone, but as the string `"null"` when inside an object.
fix
Be explicit: store `'"null"'` if you need the string. Avoid relying on round-tripping of the string "null" as a top-level value.
affects: *
deprecatedBower installation is deprecated; use npm for both browser and server.
fix
Install via npm: `npm install json-storage` and bundle with your preferred tool (webpack, browserify, etc.).
affects: <=2.1.2
Errors
Common errors & fixes
TypeError: localStorage is not a constructor
In Node.js, the `localStorage` module must be required before use.
fix
Add `const localStorage = require('localStorage');` at the top of your script.
TypeError: JsonStorage.create is not a function
Importing `json-storage` as default instead of named export.
fix
Use `const { JsonStorage } = require('json-storage');` in CommonJS or `import { JsonStorage } from 'json-storage';` in ES modules.
Cannot find module 'localStorage'
The `localStorage` peer dependency is not installed.
fix
Run `npm install localStorage` (only needed for Node.js; browsers provide it natively).
Upgrade
Version history
2.1.2latest on npm
Audit
Dependencies
localStoragerequiredRequired in Node.js to provide the W3C Storage API implementation
Agent activity
23 hits · last 30 days
node
22
Amazon
1
Resources
json-storage — npm install json-storage · libregistry