Libraries for using localStorage in your Node.js Project

Manish Mandal
Nov 19 · 4 min read
Image for post
Image for post

Did you ever tried using localStorage in your node js project and got an undefined error? Of course, you will get an undefined error because node js is a server-side technology and localStorage is a client-side feature available only in the browser. But still, there are some libraries through which you can use the localStorage like feature in your node js project. So let us see how to use them.

For tutorial purposes, we will first create a simple node js project and then will see how to use those libraries one by one.

  1. Create a directory and initialize your project.
npm init -y#or yarn init -y

2. Now create three files with the names index.js, store.js, and usage.js inside your project directory.

index.js — This file will be the main file of our project.

store.js — This file will be used to store all our localStorage data.

usage.js — This file will be used to retrieve the data from our localStorage.

3. This step is optional. If you want to use import instead of require all you need is to declare "type": "module" inside your package.json file and while importing the file you need to call the file name with extension. e.g import "./store.js" instead of "./store".

node-localstorage is a drop-in substitute for the browser native localStorage API that runs on node.js. This is widely used as an alternative to localStorage for node js. You can have access to all the methods of localStorage like length, setItem, getItem, clear, etc.

Usage

Install : npm install node-localstorage or yarn add node-localstorage

Import: const {localStorage} = require("node-localstorage"); or import {localStorage} from "node-localstorage"

Store: Inside your store.js file paste the below code

Get-Item: Paste the below code inside your usage.js file.

Run: Now import both your file in the index.js file and run node index.js command in your terminal.

Image for post
Image for post
Output

This is a tiny library that can be used as an alternative to localStorage API for node js. This library is very simple to use, just import the module and start using localStorage methods.

Usage

Install : npm install localStorage or yarn add localStorage

Import: const localStorage = require("localStorage"); or import localStorage from "localStorage"

Store: Inside your store.js file paste the below code

Get-Item: Paste the below code inside your usage.js file.

Run: Now import both your file in the index.js file and run node index.js command in your terminal.

Image for post
Image for post
Output

Store2 is the most downloaded library as an alternative to use localStorage and sessionStorage for node.js. This library has some extra methods like transact, setAll, getAll, each, etc. apart from localStorage methods.

Usage

Install : npm install store2 or yarn add store2

Import: const store = require("store2"); or import store from "store2"

Store: Inside your store.js file paste the below code

Get-Item: Paste the below code inside your usage.js file.

Run: Now import both your file in the index.js file and run node index.js command in your terminal.

Image for post
Image for post

store is one more library which we can use but it’s similar to store2 with fewer methods.

Using any of these libraries you can interchange data from one file to another without importing and exporting the whole module. There are so many different ways you can use these libraries which I can’t even explain in this tutorial alone. Hope I’ll cover the advanced part in another tutorial.

Thanks for Reading. Below I have shared the GitHub repository for reference.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Manish Mandal

Written by

Fullstack developer with expertise in WordPress and React. Writing code for humans. Add me on Linkedin https://www.linkedin.com/in/manishmandal21/

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Manish Mandal

Written by

Fullstack developer with expertise in WordPress and React. Writing code for humans. Add me on Linkedin https://www.linkedin.com/in/manishmandal21/

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store