A localStorage wrapper for all browsers without using cookies or flash, providing persistent client-side storage with automatic fallback and plugin architecture
npx @tessl/cli install tessl/npm-store@2.0.0Store.js is a cross-browser localStorage wrapper library that provides persistent client-side storage without using cookies or flash. It automatically detects and falls back to the best available storage mechanism (localStorage, globalStorage, userData, etc.) for maximum browser compatibility, while offering a modular plugin architecture for extended functionality.
npm install store// Main import (legacy build with maximum compatibility)
var store = require('store')ES6 modules:
import store from 'store'Alternative builds:
// Modern browsers only (smaller bundle)
var store = require('store/dist/store.modern')
// All features included
var store = require('store/dist/store.everything')
// Version 1.x API compatibility
var store = require('store/dist/store.v1-backcompat')var store = require('store')
// Store current user
store.set('user', { name: 'Marcus', preferences: { theme: 'dark' } })
// Get current user
var user = store.get('user')
console.log(user.name) // 'Marcus'
// Get with default value
var theme = store.get('theme', 'light')
// Remove current user
store.remove('user')
// Clear all stored values
store.clearAll()
// Loop over all stored values
store.each(function(value, key) {
console.log(key, '==', value)
})Store.js is built around several key components:
Basic storage operations that work consistently across all browsers.
// Core storage methods
store.set(key, value) // Returns value, undefined removes key
store.get(key, defaultValue?) // Returns stored value or default
store.remove(key) // Removes key-value pair
store.clearAll() // Removes all stored values
store.each(callback) // Iterates over all key-value pairs
// Store properties
store.version // Library version string
store.enabled // Boolean indicating if storage works
// Store management
store.createStore(storages, plugins, namespace?) // Creates new store instance
store.addPlugin(plugin) // Adds plugin to current store
store.namespace(namespace) // Creates namespaced store
store.hasNamespace(namespace) // Checks if store has namespaceExtended functionality through the plugin system including data expiration, event handling, array operations, and more.
// Event handling (events plugin)
store.watch(key, listener) // Watch for changes, returns subscription ID
store.unwatch(subscriptionId) // Remove event subscription
store.once(key, listener) // Listen for single change event
// Expiration support (expire plugin)
store.set(key, value, expiration) // Set with expiration timestamp
store.getExpiration(key) // Get expiration timestamp
store.removeExpiredKeys() // Clean up expired keys
// Array operations (operations plugin)
store.push(key, ...values) // Array push operation
store.pop(key) // Array pop operation
store.shift(key) // Array shift operation
store.unshift(key, ...values) // Array unshift operation
// Object operations (operations plugin)
store.assign(key, ...objects) // Object.assign operation
// Value updates (update plugin)
store.update(key, updateFn) // Update value using function
store.update(key, defaultVal, updateFn) // Update with default
// Default values (defaults plugin)
store.defaults(defaultValues) // Set default values objectDifferent build configurations and storage backend management for various browser support requirements.
// Build imports
require('store') // Legacy build (maximum compatibility)
require('store/dist/store.modern') // Modern build (smaller size)
require('store/dist/store.everything') // All features build
// Custom store creation
var engine = require('store/src/store-engine')
var storages = require('store/storages/all') // All storage backends
var plugins = require('store/plugins/all') // All plugins
var customStore = engine.createStore(storages, plugins, 'myNamespace')// Callback function types
type EachCallback = (value: any, key: string) => void
type WatchCallback = (newValue: any, oldValue: any) => void
type UpdateCallback = (currentValue: any) => any
// Plugin function type
type Plugin = () => { [methodName: string]: Function }
// Storage backend interface
interface Storage {
name: string
read(key: string): string | null
write(key: string, data: string): void
each(callback: (value: string, key: string) => void): void
remove(key: string): void
clearAll(): void
}