ReferenceError: localStorage is not defined in JS, Next.js

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# ReferenceError: localStorage is not defined in JavaScript

The "ReferenceError: localStorage is not defined" occurs for multiple reasons:

  1. Using localStorage in Node.js.
  2. Using localStorage on the server (e.g. server-side rendering in Next.js).
  3. Misspelling the localStorage global variable (should be camelCase).

referenceerror localstorage is not defined

shell
ReferenceError: localStorage is not defined

The localStorage property is a property on the window object, so it isn't available on the server.

index.js
console.log(localStorage === window.localStorage); // ๐Ÿ‘‰๏ธ true localStorage.setItem('name', 'Tom'); console.log(localStorage.getItem('name')); // ๐Ÿ‘‰๏ธ "Tom"

localstorage is property on window object

If you got the error in the browser, make sure that you haven't misspelled the localStorage keyword (should be camelCase).

If you use React.js or Next.js and you need to check if you're on the browser (can use localStorage) or on the server (can't use localStorage), check that the window variable is not undefined.

index.js
if (typeof window !== 'undefined') { console.log('You are on the browser') // ๐Ÿ‘‰๏ธ can use localStorage here localStorage.setItem('name', 'Tom'); console.log(localStorage.getItem('name')); // ๐Ÿ‘‰๏ธ "Tom" } else { console.log('You are on the server') // ๐Ÿ‘‰๏ธ can't use localStorage }
The code for this article is available on GitHub
We check if the global window variable doesn't have a type of undefined. If the window variable is defined, we are on the browser and can use the localStorage API.

The if block runs only if we are in a browser environment and the window object is defined.

If you use React.js, you can also use localStorage in your useEffect hook or in an event handler.

App.js
import {useEffect} from 'react'; export default function App() { useEffect(() => { console.log(localStorage.getItem('counter')); }, []); return ( <div> <h2>hello world</h2> <button onClick={() => { localStorage.setItem('counter', 100); }} > Increment counter </button> </div> ); }

The useEffect() hook is only run on the browser and never runs on the server.

We also used the localStorage.setItem() method in an event handler which is also allowed.

The most commonly used localStorage methods are setItem() and getItem().

index.js
// ๐Ÿ‘‡๏ธ adds the key and value to the localStorage object localStorage.setItem('key', 'value') // ๐Ÿ‘‡๏ธ returns the value of the specified key or null if the key doesn't exist localStorage.getItem('key')

# ReferenceError: localStorage is not defined in Next.js

The "ReferenceError: localStorage is not defined" often occurs in Next.js applications when you try to access localStorage before the browser renders it.

This could be in the body of a React.js component.

App.js
import {useState} from 'react'; export default function App() { // โ›”๏ธ this would run before the browser is ready (BAD) const [name, setName] = useState(localstorage.getItem('name')) return ( <div> <h2>hello world</h2> </div> ); }

You can pass null as the initial value to the useState hook and access localStorage in your useEffect hook.

The useEffect hook runs after React renders your component in the browser environment, so you can safely use the localStorage API in useEffect.

App.js
import {useEffect} from 'react'; export default function App() { useEffect(() => { console.log(localStorage.getItem('counter')); }, []); return ( <div> <h2>hello world</h2> <button onClick={() => { localStorage.setItem('counter', 100); }} > Set counter </button> </div> ); }

If that doesn't work, you have to use an if statement to check if you're on the browser (can use localStorage) or server (can't use localStorage).

index.js
if (typeof window !== 'undefined') { console.log('You are on the browser') // ๐Ÿ‘‰๏ธ can use localStorage here localStorage.setItem('name', 'Tom'); console.log(localStorage.getItem('name')); // ๐Ÿ‘‰๏ธ "Tom" } else { console.log('You are on the server') // ๐Ÿ‘‰๏ธ can't use localStorage }
The code for this article is available on GitHub

# Conclusion

To solve the "ReferenceError: localStorage is not defined" error, make sure to only use the localStorage object in the browser.

The localStorage property is defined on the window object and is not available on the server, e.g. in Node.js or when using server-side rendering with Next.js.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev