Skip to content
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

TypeError: Cannot read property 'localStorage' of undefined #744

Closed
devwojtek opened this issue Apr 26, 2018 · 6 comments · Fixed by #746
Closed

TypeError: Cannot read property 'localStorage' of undefined #744

devwojtek opened this issue Apr 26, 2018 · 6 comments · Fixed by #746

Comments

@devwojtek
Copy link

devwojtek commented Apr 26, 2018

I installed auth0-js ( version 9.5.0 ) in node application.
Node version is 6.9.0.

I got the issue after launching application with npm start.

TypeError: Cannot read property 'localStorage' of undefined
....
Can't use localStorage. Using CookieStorage instead.

How can I resolve it?

@phlogisticfugu
Copy link

also ran into this issue.

we're loading a universal js app (nuxt.js) and its happening when importing the 'auth0-js' library. The reason is that the auth0-js module is itself trying to instantiate a StorageHandler, and there is no window object on the server.

think that this should be refactored:

var storage = new StorageHandler();

to just define functions, but not actually run the code that depends upon the window object

@luisrudge
Copy link
Contributor

Auth0.js is meant to be used only inside the browser. If you are running a server rendered application, make sure to only instantiate Auth0.js objects when you're running in the browser. For react, for example, make sure to always instantiate objects in the componentDidMount hook.

@phlogisticfugu
Copy link

phlogisticfugu commented Apr 26, 2018

@luisrudge actually the issue is a little more subtle than that

Re: make sure to only instantiate Auth0.js objects when you're running in the browser

In my case, we are only running the actual call to auth0js.WebAuth() in the browser, and not server side. This error is being triggered not by that line, but instead in our library import: import auth0js from 'auth0-js'

In the particular universal js framework we are using (nuxt.js) imports are done as part of pre-processing bundling, and so the same libraries are loaded on the browser and client. Loading a module should not result in the execution of code. Rather, executing the functions defined in a module should execute code.

see

var storage = new StorageHandler();

which is running code upon module load.

@phlogisticfugu
Copy link

also note that this error only started appearing in version 9.5.0 of auth0-js

phlogisticfugu added a commit to phlogisticfugu/auth0.js that referenced this issue Apr 27, 2018
to avoid instantiating the StorageHandler, which requires the global window object, upon module load

bug was introduced in auth0#737

addresses issue: auth0#744
@phlogisticfugu
Copy link

@luisrudge I made #745 to fix this.

@luisrudge luisrudge reopened this Apr 27, 2018
@luisrudge
Copy link
Contributor

@phlogisticfugu you're totally right. I'll fix this in a different PR, since we don't want to have that lazy thing again, but thanks for figuring out the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants