auth0.js: Import fails on node.js because window is not defined

Description

import auth0 from "auth0-js"; causes an exception on nodejs

Prerequisites

Environment

Please provide the following:

  • Version of Auth0.js used: 9.11.1
  • Browser and OS version(s) affected: Any nodejs
  • Additional libraries or browser extensions being used that might affect behavior: N/A

Reproduction

$ yarn add auth0-js
$ node
> const auth0 = require("auth0-js");

Output:

Thrown:
ReferenceError: window is not defined

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 23 (10 by maintainers)

Most upvoted comments

I had a nice conversation with @timneutkens and @developit and they helped me finding a solution for this. We’ll fix this in the next release: https://github.com/auth0/idtoken-verifier/pull/42

Thanks for your patience 🎉

Hi everyone. This was fixed in the latest version. Thanks for your patience 🎉

import { useEffect } from "react";
import { WebAuth } from "auth0-js";

function Home() {
  useEffect(() => {
    const auth = new WebAuth({
      domain: "test.auth0.com",
      clientID: "alsdhalskhd02h0"
    });
  }, []);
  return <div>Welcome to Next.js!</div>;
}

export default Home;

We’ll release a new version on Monday.

I think this is an issue with auth0-js.

First of all, this problem has been recently introduced, since 9.10.4 works great, 9.11.x causes my builds to break. When using ^9.10.4, 9.11.x will be installed automatically. So if no lock file used, builds will break.

Second, it really is not hard to check for window to exist (typeof window !== ‘undefined’).

Third, there is valid reasons to use auth0-js in server rendered apps. Cases where you may for example show a placeholder in your sidebar and then, on client side, if user is logged in, render the avatar image. The whole stuff that is user specific can easily be lazy loaded on the client side after initial render.

This library is not meant to be used in nodejs. Checkout https://github.com/auth0/node-auth0

Working now. Thanks!!

Why is this issue closed without resolution? We ran into the same problem on what was expected to be just a minor version bump with no breaking changes. Our web app is also an isomorphic react app.