Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with @sidebase/nuxt-auth and next-auth: next-auth/core Import Fails + Runtime Error #953

Open
PawelRod opened this issue Dec 5, 2024 · 3 comments
Labels
question A question about NuxtAuth

Comments

@PawelRod
Copy link

PawelRod commented Dec 5, 2024

Environment

Nuxt version: 3.14.1592
Nitro version: 2.10.4
@sidebase/nuxt-auth version: 0.9.4
next-auth version: 4.21.1
Node.js version: 20.18.1 (LTS)
npm version: 9.8.1
OS: macOS 15.0.1

Reproduction

  1. Install dependencies:

npm install @sidebase/nuxt-auth@0.9.4 next-auth@4.21.1

  1. Configure nuxt.config.ts:
import vuetify from 'vite-plugin-vuetify';

export default defineNuxtConfig({
  ssr: true,
  modules: ["@sidebase/nuxt-auth"],
  vite: {
    resolve: {
      alias: {
        "next-auth/core": "next-auth",
      },
    },
  },
  nitro: {
    preset: "node-server",
    externals: {
      inline: ["@sidebase/nuxt-auth", "next-auth"],
    },
  },
  runtimeConfig: {
    authSecret: process.env.AUTH0_CLIENT_SECRET || "super-secret-key",
    public: {
      auth0Domain: process.env.AUTH0_DOMAIN,
      auth0ClientId: process.env.AUTH0_CLIENT_ID,
      auth0Audience: process.env.AUTH0_AUDIENCE,
      apiPath: process.env.API_PATH,
    },
  },
});
  1. Add the following API routes:
  • server/api/auth/[...].ts:
import Auth0Provider from "next-auth/providers/auth0";
import { NuxtAuthHandler } from "#auth";

export default NuxtAuthHandler({
  secret: process.env.AUTH0_CLIENT_SECRET || "my-superb-secret",
  providers: [
    Auth0Provider.default({
      clientId: process.env.AUTH0_CLIENT_ID,
      clientSecret: process.env.AUTH0_CLIENT_SECRET,
      issuer: process.env.AUTH0_DOMAIN,
    }),
  ],
});
  • server/api/token.get.ts:
import { getToken } from "#auth";
export default eventHandler(async (event) => {
  const token = await getToken({ event });
  return token || "no token present";
});
  1. Run the development server:

npm run dev

  1. Observe the warnings and errors in the terminal.

Describe the bug

When using @sidebase/nuxt-auth with next-auth, the following issues arise:

  1. Warnings during build:
WARN  [plugin node-resolve] Could not resolve import "next-auth/core" in /node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.js using exports defined in /node_modules/next-auth/package.json.
WARN  "next-auth/core" is imported by "node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.js", but could not be resolved – treating it as an external dependency.
  1. Runtime error during server startup:
	ERROR [worker reload] [worker init] /path/to/project/.nuxt/dev/index.mjs failed
ERROR [nitro] [uncaughtException] _interopRequireDefault$1 is not a function
  at <anonymous> (node_modules/next-auth/utils/logger.js:12:24)

These errors persist across various configurations and attempts to resolve the next-auth/core module.

Additional context

• I have tried multiple versions of next-auth (e.g., 4.21.1, 4.24.10) and the issue persists.
• Alias for next-auth/core → next-auth was added in vite.resolve.alias.
• Ensured all environment variables for Auth0 are set correctly:

AUTH0_DOMAIN=example-domain.auth0.com
AUTH0_CLIENT_ID=example-client-id
AUTH0_CLIENT_SECRET=example-client-secret
AUTH0_AUDIENCE=https://example-api.com/
API_PATH=https://example-api.com

Request for Assistance

  1. Is there a specific version of next-auth required for @sidebase/nuxt-auth@0.9.4?
  2. Are there any additional steps to resolve the next-auth/core import issue?
  3. Is this configuration missing anything critical for proper functionality?

Logs

Build Warnings

 WARN  [plugin node-resolve] Could not resolve import "next-auth/core" in /node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.js using exports defined in /node_modules/next-auth/package.json.
 WARN  "next-auth/core" is imported by "node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.js", but could not be resolved – treating it as an external dependency.


Runtime Errors

 ERROR  [worker reload] [worker init] /path/to/project/.nuxt/dev/index.mjs failed
 ERROR  [nitro] [uncaughtException] _interopRequireDefault$1 is not a function
   at <anonymous> (node_modules/next-auth/utils/logger.js:12:24)
@PawelRod PawelRod added bug A bug that needs to be resolved pending An issue waiting for triage labels Dec 5, 2024
@phoenix-ru
Copy link
Collaborator

Hi @PawelRod, you don't need to set any aliases to get next-auth to work. You should only make sure that next-auth is exactly 4.21.1. The command you provided for installing can be tricky and can actually pull a newer version without you realizing it (npm is particularly annoying with these kind of things).

Please, check the output using npm ls next-auth. Here's an example output in a real project (some paths have been cut)

$ npm ls next-auth
n@ /home/
├─┬ @next-auth/prisma-adapter@1.0.7 -> ./node_modules/.pnpm/@next-auth+prisma-adapter@1.0.7_@prisma+client@5.22.0_prisma@5.22.0__next-auth@4.21.1_next@13_kjyvrooqgjki5pckre45ojg5hy/node_modules/@next-auth/prisma-adapter
│ └── next-auth@4.21.1 invalid: "4.22.1" from node_modules/.pnpm/@next-auth+prisma-adapter@1.0.7_@prisma+client@5.22.0_prisma@5.22.0__next-auth@4.21.1_next@13_kjyvrooqgjki5pckre45ojg5hy/node_modules/@next-auth/prisma-adapter -> ./node_modules/.pnpm/next-auth@4.21.1_next@13.5.6_@babel+core@7.25.2_@opentelemetry+api@1.9.0_react-dom@18.3.1_rea_6fwbqij6liwxwszsxd4tmd4mai/node_modules/next-auth
├─┬ @sidebase/nuxt-auth@0.9.4 -> ./node_modules/.pnpm/@sidebase+nuxt-auth@0.9.4_encoding@0.1.13_magicast@0.3.4_next-auth@4.21.1_next@13.5.6_@babel+_23djppy5ki5xgoq4gsf4cwwf5m/node_modules/@sidebase/nuxt-auth
│ └── next-auth@4.21.1 -> ./node_modules/.pnpm/next-auth@4.21.1_next@13.5.6_@babel+core@7.25.2_@opentelemetry+api@1.9.0_react-dom@18.3.1_rea_6fwbqij6liwxwszsxd4tmd4mai/node_modules/next-auth
└── next-auth@4.21.1 -> ./node_modules/.pnpm/next-auth@4.21.1_next@13.5.6_@babel+core@7.25.2_@opentelemetry+api@1.9.0_react-dom@18.3.1_rea_6fwbqij6liwxwszsxd4tmd4mai/node_modules/next-auth

@PawelRod
Copy link
Author

PawelRod commented Dec 15, 2024

Hi @phoenix-ru,

Thank you for your help. Delete aliases from nuxt.config.ts, installing the packages using pnpm and adjusting the imports for NuxtAuthHandler resolved the previous issue. However, I am now encountering a new error:

ERROR  RollupError: [plugin impound] Vue app aliases are not allowed in server runtime.
[importing #build/nuxt.config.mjs from node_modules/.pnpm/nuxt@3.14.1592_@parcel+watcher@2.5.0_@types+node@22.10.2_eslint@9.17.0_jiti@2.4.1__ioredis@5._heq2owldnxdyzjcan6fxcxgemi/node_modules/nuxt/dist/app/nuxt.js]

It seems there is an issue with Vue app aliases being used in the server runtime. Could you advise on the correct way to resolve this?

I tried to set openAPI to true/false and nuxi force update, but it also not working:

  nitro: {
    experimental: {
      openAPI: false
    },
    preset: 'node-server',
  },

@phoenix-ru phoenix-ru added question A question about NuxtAuth and removed bug A bug that needs to be resolved pending An issue waiting for triage labels Dec 19, 2024
@phoenix-ru
Copy link
Collaborator

Hi again @PawelRod , nice to know that the initial issue was solved. Regarding the issue you are facing, it doesn't appear to be related to our module, but probably because of your setup, see nuxt/nuxt#14869 or nuxt-modules/i18n#3111 for a similar issue.

We haven't yet encountered it on any of our projects, so you may try downgrading Nuxt or NuxtAuth to see if that solves the issue. I have a feeling it is related to one of the other modules you are installing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question about NuxtAuth
Projects
None yet
Development

No branches or pull requests

2 participants