Skip to content

ERR_REQUIRE_ESM on vite build when using '@auth0/nextjs-auth0/edge' #1752

Closed
@awwong1

Description

@awwong1

Checklist

Description

This issue arises when using nextjs-auth0 with Vite and is not relevant to NextJS.

When import { initAuth0 } from '@auth0/nextjs-auth0/edge' is encountered while using vite, the following error is thrown:

[vite] Error when evaluating SSR module /opt/buildhome/auth0-sveltekit-edge-issue/src/routes/api/auth/[auth0]/+server.ts: failed to import "@auth0/nextjs-auth0/edge"
|- Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/oauth4webapi/build/index.js from /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js not supported.
Instead change the require of index.js in /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js:5:36)

When building:

> [email protected] build
> vite build

vite v5.2.11 building SSR bundle for production...
"confetti" is imported from external module "@neoconfetti/svelte" but never used in "src/routes/sverdle/+page.svelte".
✓ 110 modules transformed.

/opt/buildhome/auth0-sveltekit-edge-issue/node_modules/wrangler/wrangler-dist/cli.js:29749
            throw a;
            ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/oauth4webapi/build/index.js from /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js not supported.
Instead change the require of index.js in /opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/opt/buildhome/auth0-sveltekit-edge-issue/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js:5:36)
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37)
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:814:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.10.0

Expected behaviour is that the edge variant of this library can be imported without error.

Reproduction

Minimal reproducible example: https://github.com/awwong1/auth0-sveltekit-edge-issue

  1. git clone https://github.com/awwong1/auth0-sveltekit-edge-issue.git && cd auth0-sveltekit-edge-issue
  2. npm i
  3. Create the .dev.vars file containing APP_URL, AUTH0_SECRET, AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET environment variables
  4. Add http://localhost:5173/api/auth/callback to your Auth0 application allowed callback URLs
  5. npm run dev, observe that http://localhost:5173/api/auth/login, http://localhost:5173/api/auth/callback, http://localhost:5173/api/auth/me, http://localhost:5173/api/auth/logout all function appropriately
  6. Replace initAuth0 with /edge variant , observe build/run errors

Additional context

Addressing this issue would help unlock using Auth0 with Sveltekit in the server side rendered edge environment.

nextjs-auth0 version

3.5.0

Next.js version

14.2.3

  • nextjs is not used in the reproducible example, but this version number was pulled from the node_modules/@next/env/package.json, npm explain @next/env
@next/[email protected] peer
node_modules/@next/env
  @next/env@"14.2.3" from [email protected]
  node_modules/next
    peer next@">=10" from @auth0/[email protected]
    node_modules/@auth0/nextjs-auth0
      @auth0/nextjs-auth0@"^3.5.0" from the root project

Node.js version

v20.10.0

EDIT: might be a duplicate of #1714 also oauth4webapi explicitly states commonjs is out of scope for their library https://github.com/panva/oauth4webapi/#out-of-scope

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions