Open awwong1 opened 5 months ago
same here on webpack Next.js
Import trace for requested module:
./node_modules/@auth0/nextjs-auth0/dist/edge.js
./utils/auth/node.ts
./app/api/auth/[auth0]/route.ts
⨯ ./node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js:5:1
Module not found: ESM packages (oauth4webapi) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals
EDIT: setting experimental: { esmExternals: "loose" }
workaround(?) fixes the issue
funnily enough we run prod app with same config as this one and no error is present eg. one app - type module, 3.5.0 this package - 2.10.3 oauth4web, node 20, webpack - works other app (freshly created via create-next-app) - type module, 3.5.0 - 2.10.3 oauth4web, node 20, webpack - errors on import
I was also able to workaround the ERR_REQUIRE_ESM error by building panva/oauth4webapi as a commonjs module. See patch:
diff --git a/package.json b/package.json
index 8fce4f2..1419e1f 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,6 @@
"license": "MIT",
"author": "Filip Skokan <panva.ip@gmail.com>",
"sideEffects": false,
- "type": "module",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
diff --git a/tsconfig.json b/tsconfig.json
index 7d1a54f..8a03280 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -3,7 +3,7 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"target": "ES2020",
- "module": "ES2020",
+ "module": "commonjs",
"lib": ["ES2020", "DOM", "DOM.Iterable", "ES2022.Error", "ES2021.Promise"],
"strict": true,
"removeComments": true,
@@ -13,7 +13,6 @@
"noImplicitAny": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
- "verbatimModuleSyntax": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
After linking the dependency, this reveals another issue as the
EDIT: fs/url import was due to dev artifacts, does not appear with only dist assets and with oauth4webapi built for commonjs.@auth0/nextjs-auth0/edge
import appears to still use node modules 'fs' and 'url'.
EDIT: error is no longer present with @awwong1/oauth4webapi@1.0.0, @awwong1/nextjs-auth0@1.0.1
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 usingvite
, the following error is thrown:When building:
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
.dev.vars
file containingAPP_URL
,AUTH0_SECRET
,AUTH0_DOMAIN
,AUTH0_CLIENT_ID
,AUTH0_CLIENT_SECRET
environment variableshttp://localhost:5173/api/auth/callback
to your Auth0 application allowed callback URLsnpm 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 appropriatelyinitAuth0
with/edge
variant , observe build/run errorsAdditional 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
node_modules/@next/env/package.json
,npm explain @next/env
Node.js version
v20.10.0
EDIT: might be a duplicate of https://github.com/auth0/nextjs-auth0/issues/1714 also
oauth4webapi
explicitly states commonjs is out of scope for their library https://github.com/panva/oauth4webapi/#out-of-scope