question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

localStorage is not defined.

See original GitHub issue

I guess this is due to lack of localStorage support in jsdom.

Starting` crawling http://localhost:59817/ Failed to retrieve initialize state from localStorage: ReferenceError: localStorage is not defined at http://localhost:59817/static/js/main.923fa5f4.js:24:24448 at http://localhost:59817/static/js/main.923fa5f4.js:24:26074 at o (http://localhost:59817/static/js/main.923fa5f4.js:13:23313) at Object.<anonymous> (http://localhost:59817/static/js/main.923fa5f4.js:14:24180) at t (http://localhost:59817/static/js/main.923fa5f4.js:1:107) at Object.<anonymous> (http://localhost:59817/static/js/main.923fa5f4.js:1:505) at t (http://localhost:59817/static/js/main.923fa5f4.js:1:107) at http://localhost:59817/static/js/main.923fa5f4.js:1:195 at http://localhost:59817/static/js/main.923fa5f4.js:1:200 at ContextifyScript.Script.runInContext (vm.js:32:29) Unable to persist state to localStorage: ReferenceError: localStorage is not defined at Array.<anonymous> (http://localhost:59817/static/js/main.923fa5f4.js:24:24646) at d (http://localhost:59817/static/js/main.923fa5f4.js:13:22744) at http://localhost:59817/static/js/main.923fa5f4.js:24:306 at Object.dispatch (http://localhost:59817/static/js/main.923fa5f4.js:24:25796) at t.r.handleLocationChange (http://localhost:59817/static/js/main.923fa5f4.js:23:31542) at t.componentWillMount (http://localhost:59817/static/js/main.923fa5f4.js:23:31796) at c.performInitialMount (http://localhost:59817/static/js/main.923fa5f4.js:21:17205) at c.mountComponent (http://localhost:59817/static/js/main.923fa5f4.js:21:16373) at Object.mountComponent (http://localhost:59817/static/js/main.923fa5f4.js:3:7565) at c.performInitialMount (http://localhost:59817/static/js/main.923fa5f4.js:21:17487) ✏️ Saving / as /index.html

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:14
  • Comments:16

github_iconTop GitHub Comments

7reactions
patelrikincommented, Feb 27, 2020

I solved it by adding local-storage polyfill for development dependency

npm i -D localstorage-polyfill OR yarn add -D localstorage-polyfill

In your App’s index.js file add following: import 'localstorage-polyfill'

This solved error for me.

5reactions
arefaslanicommented, Sep 11, 2017

You should not use localStorage instance in the component’s render or any method that is evaluated in the server. Move localStorage calls in the componentWillMount method for example. I’ve solved my problem this way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - “localStorage is not defined” error showing
After this, I am getting the following error: ReferenceError: localStorage is not defined. Please help how to resolve it. My package.json :
Read more >
ReferenceError: localStorage is not defined in JavaScript
To solve the "ReferenceError: localStorage is not defined" error, make sure to only use the localStorage object in the browser. The localStorage property...
Read more >
How to Fix "localStorage is not defined" in Next.js
This is because localStorage is not defined on the window object, and Next. js performs a server render before the client render.
Read more >
localStorage is not defined - Next.js · Discussion #19911
Hi @danilo-leal. The issue you are seeing is because localStorage (aka window.localStorage ) is not defined on the server side. Next server ...
Read more >
Why do I get "ReferenceError: localStorage is not defined ...
Hi. I have this code const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem('token'))). But it throws this error:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found