Auth0 Single Page App Confusion

Hi again!

Great to hear you managed to make progress with the indefinite loading screen created by the protected page!

Just to confirm, have you imported the respective createAuth0Client() within your application as such:

import { createAuth0Client } from '@auth0/auth0-spa-js';

From what I understand the root cause of the error message “createAuth0Client is not defined” would be due to the fact that it is not being imported from the Auth0 SPA JS sdk within your codebase, causing the whole authorization process to fail due to the severed connection to your Auth0 application and tenant.

If you can either post your code within the thread (make sure to sanitize any sensitive information) or you can send me a DM.

Kind Regards,
Nik

Kind Regards,
Nik

Hi Nik,

Let me explain.

In an earlier post you made this comment to me:

So I tried to use your suggestion to build a version of my app that would work on my GoDaddy server.

  1. I inserted a CDN script tag into the index.html file in the . I used your script that you provided in an earlier posting.
  2. I removed the Import Auth0 SPA JS from the app.js file and modified the Auth0Client code as follows:

let auth0Client = null;
window.onload = async () => {
auth0Client = await auth0.createAuth0Client({
domain: “YOUR_AUTH0_DOMAIN”,
clientId: “YOUR_CLIENT_ID”,
authorizationParams: {
redirect_uri: window.location.origin
}
});

I tried to run “npm run build” in my terminal but I received this error message in my terminal:

PS C:\PECBMembersProd> npm run build

pecbmembersprod@1.0.0 build
vite build
vite v8.0.13 building client environment for production…

✓ 5 modules transformed.
✗ Build failed in 269ms
error during build:

Build failed with 1 error:
[PARSE_ERROR] Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[ app.js:31:4 ]

31 │ } catch (err) {
│ │
│ ╰─

│ Help: Try inserting a semicolon here
────╯

at aggregateBindingErrorsIntoJsError (file:///C:/PECBMembersProd/node_modules/rolldown/dist/shared/error-CkdMJ9ps.mjs:48:18)
at unwrapBindingResult (file:///C:/PECBMembersProd/node_modules/rolldown/dist/shared/error-CkdMJ9ps.mjs:18:128)
at #build (file:///C:/PECBMembersProd/node_modules/rolldown/dist/shared/rolldown-build-BVD3dIdE.mjs:3275:34)
at async buildEnvironment (file:///C:/PECBMembersProd/node_modules/vite/dist/node/chunks/node.js:33133:64)
at async Object.build (file:///C:/PECBMembersProd/node_modules/vite/dist/node/chunks/node.js:33555:19)
at async Object.buildApp (file:///C:/PECBMembersProd/node_modules/vite/dist/node/chunks/node.js:33552:153)
at async CAC.<anonymous> (file:///C:/PECBMembersProd/node_modules/vite/dist/node/cli.js:777:3) {

errors: [Getter/Setter]
}

Apparently Vite doesn’t like the syntax that you need to load Auth0 SPA JS when you’re running in a browser environment.

Is there a way to work around these issues and use Vite to build the code that does run in a browser?

Kind regards,

Warren

Hi again @wsmetz72

I am sorry for my delayed response, I have been out of office and could not get back to you in time.

When you use Vite, you must use NPM imports (like import { createAuth0Client } from '@auth0/auth0-spa-js'; ) and completely remove the Auth0 CDN link from your HTML. Furthermore, the randomly generated file names seen after running npm run build are intentional and must not be manually renamed, as Vite automatically wires them into the final production index.html .

The createAuth0Client is not defined / Parse Errors
If a developer relies on a CDN (<script src="https://cdn.auth0.com/..."></script> ), the auth0 object is automatically attached to the global window . Vite, however, is a strict ES Module bundler. During the npm run build step, Vite statically analyzes the app.js code. If it sees a call to createAuth0Client() without an explicit import statement at the top of the file, Vite has no idea where that function comes from. This leads to “not defined” errors at runtime, or parsing errors during the build if the code attempts to mix global window variables with module imports.
The File Naming Issue (Cache Busting)
When the user successfully built the project, they noticed files like app-rlbWHTKs.js and manually renamed them back to app.js .
Vite intentionally appends that random string (a hash) to the file names. This is a critical web performance and caching practice called Cache Busting . It guarantees that when the dist folder is uploaded to GoDaddy, the end-users’ browsers will download the new, updated file instead of loading an outdated, cached version of app.js from their local browser history. By manually renaming the files, the user broke the links that Vite automatically injected inside the compiled index.html , resulting in a blank page or a broken application.

SOLUTION:

To resolve the confusion in the thread and deploy successfully, you must embrace the Vite (NPM) workflow:

1. Install the SDK via NPM
Ensure the SDK is actually installed in the project folder, not just linked in the HTML.

npm install @auth0/auth0-spa-js

2. Clean up index.html
Remove the Auth0 CDN <script> tag from the HTML file entirely. The application should only load the main JavaScript module:

<script type="module" src="/app.js"></script>

3. Import Correctly in app.js
At the very top of the app.js file, explicitly import the client creation function. Because it is imported directly, you drop the auth0. prefix when calling it:

import { createAuth0Client } from '@auth0/auth0-spa-js';

let auth0Client;
window.onload = async () => {
    auth0Client = await createAuth0Client({
        domain: import.meta.env.VITE_AUTH0_DOMAIN,
        clientId: import.meta.env.VITE_AUTH0_CLIENT_ID,
        authorizationParams: {
            redirect_uri: window.location.origin
        }
    });
    // ... continue application logic
};

  1. Build and Deploy (Without Renaming!)

  2. Run npm run build in the terminal.

  3. Vite will generate a dist folder containing a new index.html and an assets folder (holding the securely hashed files).

  4. Take the entire contents of the dist folder and upload them directly to the hosting provider (e.g., GoDaddy). Do not rename any files.

This should do the trick and have you app up and running.

Kind Regards,
Nik