{"id":49265,"library":"safe-storage","title":"safe-storage","description":"safe-storage (v1.0.2) provides drop-in replacements for localStorage and sessionStorage that gracefully fall back to an in-memory implementation when the native browser storages are unavailable or throw errors (e.g., Safari private mode). No dependencies, no build step, and supports both ESM and CommonJS. Unlike other storage wrappers, it specifically targets the Safari private browsing bug and does not introduce any new API surface — it exactly mirrors the native Storage interface. Released in 2017, stable with no recent updates.","status":"maintenance","version":"1.0.2","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","localStorage","sessionStorage","in-memory"],"install":[{"cmd":"npm install safe-storage","lang":"bash","label":"npm"},{"cmd":"yarn add safe-storage","lang":"bash","label":"yarn"},{"cmd":"pnpm add safe-storage","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export does not exist; must use named import.","wrong":"import safeLocalStorage from 'safe-storage'","symbol":"safeLocalStorage","correct":"import { safeLocalStorage } from 'safe-storage'"},{"note":"Library supports both ESM and CJS, but the CJS require pattern is less common in modern codebases.","wrong":"const { safeSessionStorage } = require('safe-storage')","symbol":"safeSessionStorage","correct":"import { safeSessionStorage } from 'safe-storage'"},{"note":"Storage is a TypeScript type export, not a runtime value. Use 'import type' for type-only imports.","wrong":"import { Storage } from 'safe-storage'","symbol":"Storage (type)","correct":"import type { Storage } from 'safe-storage'"}],"quickstart":{"code":"import { safeLocalStorage, safeSessionStorage } from 'safe-storage';\n\nsafeLocalStorage.setItem('user', JSON.stringify({ name: 'Alice' }));\nconst user = JSON.parse(safeLocalStorage.getItem('user') ?? 'null');\nconsole.log(user.name); // Alice\n\nsafeSessionStorage.setItem('sessionId', 'abc123');\nconsole.log(safeSessionStorage.length); // 1\nsafeSessionStorage.clear();\nconsole.log(safeSessionStorage.length); // 0","lang":"typescript","description":"Demonstrates basic setItem, getItem, length, and clear methods for both safeLocalStorage and safeSessionStorage."},"warnings":[{"fix":"Wrap calls in a try-catch if you need to handle quota exceeded errors, or use a different library that handles that case.","message":"The library only falls back to in-memory storage when the native storage throws or is missing. If the native storage is present but full (quota exceeded), safe-storage does NOT catch that error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider using a more modern alternative like 'localforage' or 'storage-factory' that provides a similar fallback but with more features and active maintenance.","message":"This package has not been updated since 2017. It may not be compatible with newer environments or bundlers that enforce stricter module resolution.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Do not use this library on the server side for persistent data; use a dedicated database or file-based storage instead.","message":"In Node.js environments, both safeLocalStorage and safeSessionStorage will always fall back to in-memory storage because native localStorage is not available. This may cause data loss across server restarts.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'2017':81 'api':70 'back':20 'brows':62 'browser':30 'bug':63 'build':44 'commonj':51 'depend':42 'drop':10 'drop-in':9 'e.g':37 'error':36 'esm':49 'exact':73 'fall':19 'grace':18 'implement':26 'in-memori':23,90 'interfac':78 'introduc':67 'javascript':87 'localstorag':14,88 'memori':25,92 'mirror':74 'mode':40 'nativ':29,76 'new':69 'privat':39,61 'provid':8 'recent':85 'releas':79 'replac':12 'safari':38,60 'safe':2,5 'safe-storag':1,4 'sessionstorag':16,89 'specif':57 'stabl':82 'step':45 'storag':3,6,31,54,77 'support':47 'surfac':71 'target':58 'throw':35 'unavail':33 'unlik':52 'updat':86 'v1.0.2':7 'wrapper':55","created_at":"2026-06-07T17:00:53.388724+00:00","updated_at":"2026-06-07T17:00:53.388724+00:00","problems":[{"fix":"Use named import: `import { safeLocalStorage } from 'safe-storage'`","cause":"Using default import instead of named import: `import safeLocalStorage from 'safe-storage'`","error":"Uncaught TypeError: safeLocalStorage is not a function"},{"fix":"Install the package: `npm install safe-storage`. Ensure tsconfig.json includes 'node_modules' in 'types' or 'typeRoots'.","cause":"The package may not be installed, or TypeScript cannot find the type definitions.","error":"Cannot find module 'safe-storage' or its corresponding type declarations."},{"fix":"Ensure the import is at the top of the file and that the module is correctly installed. Check for circular dependencies.","cause":"Attempting to use safeLocalStorage before the import resolves, often in environments where the module is not fully loaded.","error":"TypeError: Cannot read properties of undefined (reading 'getItem')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"safe-storage","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}