Closed
Description
Checklist
- The issue
can be reproduced in the nextjs-auth0 sample app(or N/A). - I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
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
git clone https://github.com/awwong1/auth0-sveltekit-edge-issue.git && cd auth0-sveltekit-edge-issue
npm i
- Create the
.dev.vars
file containingAPP_URL
,AUTH0_SECRET
,AUTH0_DOMAIN
,AUTH0_CLIENT_ID
,AUTH0_CLIENT_SECRET
environment variables - Add
http://localhost:5173/api/auth/callback
to your Auth0 application allowed callback URLs npm run dev
, observe thathttp://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- 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.
- https://community.auth0.com/t/sveltekit-integration/72204/3
- https://community.auth0.com/t/provide-example-sdk-for-server-side-authentication-with-auth0-for-sveltekit/105041
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