Next.js Discord

Discord Forum

"authOptions" is not a valid Route export field.

Answered
German yellowjacket posted this in #help-forum
Open in Discord
German yellowjacketOP
Hi I am getting this error when trying to deploy my branch to Vercel. I wasn't able to find anything online.

Type error: Route "src/app/api/auth/[...nextauth]/route.js" does not match the required types of a Next.js Route.
"authOptions" is not a valid Route export field.

the file in questions

import { connectMongoDB } from "@/lib/mongodb";
import User from "@/models/User";
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";


export const authOptions = {
  providers: [
    CredentialsProvider({
      name: "credentials",
      credentials: {},

      async authorize(credentials) {
        const { email, password } = credentials;

        try {
          await connectMongoDB();
          const user = await User.findOne({ email });

          if (!user) {
            return null;
          }

          const passwordsMatch = await bcrypt.compare(password, user.password);

          if (!passwordsMatch) {
            return null;
          }

          return user;
        } catch (error) {
          console.log("Error: ", error);
        }
      },
    }),
  ],
  session: {
    strategy: "jwt",
  },
  secret: process.env.NEXTAUTH_SECRET,
  pages: {
    signIn: "/", // Page your using for sign in
  },
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
Answered by Ray
eg, create a file on app/auth.options.ts and put the authOptions to there and export it
View full answer

9 Replies

@Ray try exporting `authOptions` on other file and import it to `route.js` or don't export `authOptions` inside route.js
German yellowjacketOP
not sure how i would export that on a separate file since this whole file is pretty much exporting this?
@German yellowjacket not sure how i would export that on a separate file since this whole file is pretty much exporting this?
eg, create a file on app/auth.options.ts and put the authOptions to there and export it
Answer
next doesn't allow you export anything other than route handler inside route.js
or just do const authOptions = {} if you don't use authOptions in other place
Narrow-barred Spanish mackerel
Just in case it can help, there is a PR to allow this (but with a _ prefix). If you think this is useful, any upvote or comment with your own usecase woud be appreciated