reactjs - Next.js 15 API Route - “Invalid GET Export” Error in Dynamic Route [id] - Stack Overflow

admin2025-04-17  2

I’m working on a Next.js 15 project using the App Router (/app/api/) and trying to implement a dynamic API route (/api/agencies/[id]) to fetch a specific agency from MongoDB.

However, during npm run build, I get this TypeScript error: src/app/api/agencies/[id]/route.ts Type error: Route "src/app/api/agencies/[id]/route.ts" has an invalid "GET" export: Type "{ params: { id: string; }; }" is not a valid type for the function's second argument.

What I Have Tried So Far:

  1. Modified the function signature for GET, PATCH, and DELETE requests:

export async function GET(req: NextRequest, context: { params: { id: string } }) {

❌ Still getting the “invalid GET export” error.

  1. Changed params type to Record<string, string>:

export async function GET(req: NextRequest, context: { params: Record<string, string> }) {

❌ No luck.

  1. Tried wrapping params in an optional object:

export async function GET(req: NextRequest, context: { params?: { id?: string } }) {

❌ Still getting the same error.

  1. Checked TypeScript compatibility in tsconfig.json:
"target": "esnext",
"module": "esnext",
"strict": true,

• No improvements.

  1. Tried awaiting params before using it:

const { id } = await context.params;

❌ Still doesn’t work.

转载请注明原文地址:http://anycun.com/QandA/1744847759a88456.html