Skip to content

Guide · Open source

How to use store.js: install, get, set and plugins

Updated · written with AI from the project's own documentation and checked against our catalogue data

store.js is a JavaScript library that gives web pages one simple interface for saving and reading data in the browser. It picks the best available storage mechanism and falls back to others when needed, so the same code can run across many browsers. This guide shows you how to install it, use its core API, choose a build and add optional plugins.

store.jsmarcuswestin/store.js · ★ 14k · MIT

Quick start

  1. 1Install the package. Run npm i store in your project if you use npm. Node.js is required only for this route.
  2. 2Call set and get. Save a value with store.set and read it back with store.get to confirm it works in your browser.
  3. 3Choose a build. Pick the legacy build for old browsers or the modern build to save size on modern browsers.
  4. 4Add the plugins you need. Require a plugin such as expire or events and register it with store.addPlugin.
  5. 5Check storage limits. Read the storage limits and recommendations in the README before saving large data.

Install store.js with npm

If you use npm, install the library with the package-manager command below. Node.js is only required if you are using npm.

npm i store

Use the core get, set, remove and each methods

store.js provides basic key/value storage through a small API. You save a value with set, read it with get, delete it with remove, and loop over stored values with each. The README also lists clearAll for removing all keys.

  • set(key, value) stores a value, including objects.
  • get(key) returns the stored value.
  • remove(key) deletes one key.
  • clearAll() removes all keys.
  • each(function(value, key)) loops over all stored values.
// Store current user store.set('user', { name:'Marcus' }) // Get current user store.get('user') // Remove current user store.remove('user') // Clear all keys store.clearAll() // Loop over all stored values store.each(function(value, key) { console.log(key, '==', value) })

Load store.js with npm or a script tag

With npm, require the library in your code and then call the API. If you are not using a bundler, download one of the builds and include it with a script tag. The script tag example uses the legacy minified build.

// Example store.js usage with npm var store = require('store') store.set('user', { name:'Marcus' }) store.get('user').name == 'Marcus'

Choose the right build for your browsers

The default require('store') and store.legacy.min.js target all tested browsers, including old versions such as IE 6. To save some kilobytes while still supporting all modern browsers, use the modern build instead. The everything build includes all plugins and all storages.

  • store.legacy.min.js: full support for all tested browsers; add plugins separately.
  • store.modern.min.js: full support for all modern browsers; add plugins separately.
  • store.everything.min.js: all the plugins and all the storages.
  • store.v1-backcompat.min.js: backwards compatibility with store.js v1.
var store = require('store/dist/store.modern')

Add plugins for expiry, defaults and change events

Plugins add optional features. The README lists expire for expiring values at a given time, defaults for declaring default values, and events for being notified when stored values change. To use a plugin with npm, require it and pass it to addPlugin. If you use script tags, you can use the everything build or clone the repo and run make build.

The example below stores a value that expires in 3 seconds using a custom build that includes the expire plugin.

  • events.js: get notified when stored values change.
  • expire.js: expire stored values at a given time.
  • defaults.js: declare default values.
  • observe.js, dump.js, operations.js and update.js are also listed.
var expirePlugin = require('store/plugins/expire') store.addPlugin(expirePlugin)

Understand storage fallback and limits

Store.js picks the best available storage and automatically falls back to the first available storage that works. The README lists localStorage, sessionStorage, cookieStorage, memoryStorage and older options. Cookie storage is noted as useful for Safari Private mode, and memory storage as a fallback to keep store functionality at all times.

Each storage has different limits and overflow behavior depending on the browser. The README gives Android localStorage limits as an example and then provides cross-browser recommendations in a table. Check that table before storing large amounts of data.

  • localStorage: great for all modern browsers.
  • sessionStorage: stores values in sessionStorage.
  • cookieStorage: useful for Safari Private mode.
  • memoryStorage: a fallback to ensure functionality at all times.

What people use it for

  • Remember a user's name or preferences between page loads.
  • Keep a small settings object on a site that must work on older browsers.
  • Expire a cached value after a set time using the expire plugin.
  • Get notified when a stored value changes using the events plugin.

Frequently asked questions

Is store.js free to use?

store.js is released under the MIT licence, as listed in the project details. The MIT licence is a permissive open-source licence, so check its terms for your own use.

What do I need to install store.js?

The README gives one install method: npm i store. Node.js is only required if you use npm. You can also download a build from the dist folder and include it with a script tag.

Which browsers does store.js support?

The README says it has been tested on IE6+, iOS 8+, Android 4+, Firefox 4+, Chrome 27+, Safari 5+ and Opera 11+. It also mentions testing on Node with a localStorage package. Modern-only projects can use the smaller modern build.

Can store.js save large or sensitive data?

The README does not recommend this. It notes that each storage has different limits and overflow behavior, and it does not describe encryption. Use it for small pieces of data.

Does store.js work on the server?

No, the project is aimed at browser storage. For server-side data needs, use a different tool.

What are the alternatives to store.js?

The project lists localForage, js-cookie and Dexie.js as comparable projects. Modern-only projects may find built-in localStorage enough, according to the README.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

store.js suits front-end developers who need small pieces of browser data across page loads, especially on sites that must support older browsers. If you only target modern browsers, the README suggests built-in localStorage may be enough, and large or sensitive data should go elsewhere.

Install options & downloads