-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebpackError: ReferenceError: localStorage is not defined #14480
Comments
You should see this comment: #309 (comment). Basically, when you build, |
@kajchang Is Gatsby not the right frameword for SPAs? That would be a shame because I love working with it. |
@kajchang okay, I have been able to build and deploy the site. My solution looks like this (I don't know why insert code isn't working and I don't have time to troubleshoot that too): `import * as StorageHelpers from '../js/localstorage_helpers'; function PageFramework(props) { useEffect(() => { useEffect(() => {
}, [initialized, windowLoaded]);` I hope this helps someone and thanks you @kajchang for the link! |
@JeffWScott you don't necessarily have to use state for this. |
I have another solution. Just retrieve data after a component was mounted. // Wrong
const [key] = useState(localStorage.getItem('token')) // Correct
const [key, setKey] = useState(undefined)
useEffect(() => {
setKey(localStorage.getItem('token'))
}, []) |
Thanks for helping me. |
I am using localStorage in a redux action type.
What is a good way to avoid this issue? Thanks |
@arhoy gatsby when builds your website/app. It will do it in node, some apis like |
This is just a question from a guy who is largely ignorant on how frameworks are put together, but doesn't it make sense that if this fact is largely known that the framework itself couldn't do this automatically? |
@JeffWScott it could be done, but it would be a daunting task to do this, based on my knowledge of how the framework is put together. As there are alot of moving parts inside, gatsby is already really "inteligent" in picking up some stuff, but for this case it to add guard methods, it would have to make a run checking each individual files for specific patterns and change the files accordingly, this would add a big overhead on the build time. More even, not everyone has the same coding style, so even more with that. I know that seasoned gatsby developers know how to circunvent this, but newcomers don't and with that that's why @gillkyle has put this #19966 that from my perspective will help solve issues like this in the future. The documentation already has a mention on how to solve it, but it's a bit tucked away in the corner and not all people will discover it. Most get the answer they want from a search that will lead them to a big list of similar issues, so with that the effort that was put in the pr i've mentioned will be a great addition. |
Use let variable if Local Storage does not exist As Local storage only exists in browser runtime. When developing for Gatsby, think like a script ;) |
Description
localStorage is available when running 'develop' but build fails on error "WebpackError: ReferenceError: localStorage is not defined"
Steps to reproduce
add a reference to localStorage anywhere in your gatsby project
Expected result
localStorage works
Actual result
localStorage throws an error on build
Environment
Run
gatsby info --clipboard
in your project directory and paste the output here.System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-3550 CPU @ 3.30GHz
Binaries:
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
Languages:
Python: 2.7.15
Browsers:
Edge: 42.17134.1.0
npmPackages:
gatsby: ^2.8.2 => 2.8.2
gatsby-image: ^2.1.2 => 2.1.2
gatsby-plugin-manifest: ^2.1.1 => 2.1.1
gatsby-plugin-offline: ^2.1.1 => 2.1.1
gatsby-plugin-react-helmet: ^3.0.12 => 3.0.12
gatsby-plugin-sharp: ^2.1.3 => 2.1.3
gatsby-source-filesystem: ^2.0.38 => 2.0.38
gatsby-transformer-sharp: ^2.1.21 => 2.1.21
Opening bug reports is always my last resort. I google'd the crap out of this and didn't get anywhere.
The text was updated successfully, but these errors were encountered: