Recently, I have encountered an issue with NextJS routes while working on an ecommerce project. I am seeking guidance to resolve this issue, specifically related to my route.ts file which interacts with NextAuth for providers like Google. During development, everything is functioning correctly; however, when attempting to build the project, errors are occurring. I am using NextJS 14 and following a tutorial on YouTube (link: here). The problem arose around the 5-hour mark of the tutorial when building the project... I even tried copying the source code, but without success.
In dev mode, everything is functioning smoothly. However, upon running npm run build to build the project, I encounter the following error:
Type error: Route "src/app/api/auth/[...nextauth]/route.ts" does not match the required types of a Next.js Route.
"authOptions" is not a valid Route export field.
Relevant files include:
app/api/auth/[...nextauth]/route.ts
import { mergeAnonymousCartIntoUserCart } from "@/lib/db/cart";
import { prisma } from "@/lib/db/prisma";
import { env } from "@/lib/env";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { NextAuthOptions } from "next-auth";
import { Adapter } from "next-auth/adapters";
import NextAuth from "next-auth/next";
import GoogleProvider from "next-auth/providers/google";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma) as Adapter,
providers: [
GoogleProvider({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
session({ session, user }) {
session.user.id = user.id;
return session;
},
},
events: {
async signIn({ user }) {
await mergeAnonymousCartIntoUserCart(user.id);
},
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
I am importing and using authOptions in other files, although I believe it is unnecessary to provide that code. I am open to trying out different solutions and providing additional information about my codebase if needed.
For those interested in reproducing the issue, below is a minimal reproduction code snippet. Do remember to install next-auth
. (
src/app/api/auth/[...nextauth]/route.ts
):
import { NextAuthOptions } from "next-auth";
import NextAuth from "next-auth/next";
export const authOptions: NextAuthOptions = {
providers: [],
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };