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.
-
Create a directory and initialize your project.
npm init -y
#or
yarn init -y
-
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.
- 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"
.
1. node-localstorage
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.
Output
2. localStorage
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.
Output
3. store2
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.
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.