I'm encountering an issue while working on a Next.js project. When attempting to create an optimized production build, the TypeScript compilation fails with the following error:
Compiler edge-server unexpectedly exited with code: null and signal: SIGTERM
✓ Compiled successfully
Linting and checking validity of types ..Failed to compile.
app/api/auth/[...nextauth]/route.js
Type error: Route "app/api/auth/[...nextauth]/route.js" does not match the required types of a Next.js Route.
"default" is not a valid Route export field.
Linting and checking validity of types ... (additional details, if any)
This is the full code snippet
// app/api/auth/[...nextauth]/route.js
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import client from "../../../../lib/sanityConfig";
export const authOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
async authorize(credentials, req) {
const { username, password } = credentials;
const sanityQuery = `*[ _type == "admin" && username == $username && !(_id in path('drafts.**'))] {
username,
password,_id
}[0]`;
const user = await client.fetch(sanityQuery, { username });
if (!user) {
throw new Error("admin Doesnt Exist");
}
const match = password == user.password;
if (!match) {
throw new Error("Invalid credentials");
}
return user;
},
}),
],
pages: {
signIn: "/login",
},
secret: process.env.NEXTAUTH_SECRET,
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
I'm seeking help in understanding the root cause of this TypeScript compilation error and any possible solutions or suggestions to resolve it. Has anyone encountered a similar issue with Next.js API routes and TypeScript, and how was it resolved?
What did you try: I attempted to resolve the issue by:
- Adjusting the export structure in my app/api/auth/[...nextauth]/route.js file.
- Verifying the TypeScript configuration in my tsconfig.json file.
- Searching on Google and ChatGPT for solutions, but I couldn't find a resolution.