Skip to content

Commit 3b88160

Browse files
committed
Improve Proxy invalid export error message
1 parent 68b0225 commit 3b88160

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

packages/next/src/build/analysis/get-page-static-info.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ function validateMiddlewareProxyExports(ast: any, page: string): void {
308308

309309
// Check if this is middleware/proxy
310310
const isMiddleware =
311-
page.includes(`/${MIDDLEWARE_FILENAME}`) ||
312-
page.includes(`/src/${MIDDLEWARE_FILENAME}`)
311+
page === `/${MIDDLEWARE_FILENAME}` ||
312+
page === `/src/${MIDDLEWARE_FILENAME}`
313313
const isProxy =
314-
page.includes(`/${PROXY_FILENAME}`) ||
315-
page.includes(`/src/${PROXY_FILENAME}`)
314+
page === `/${PROXY_FILENAME}` ||
315+
page === `/src/${PROXY_FILENAME}`
316316

317317
if (!isMiddleware && !isProxy) {
318318
return // Not a middleware/proxy file, no validation needed
@@ -341,18 +341,6 @@ function validateMiddlewareProxyExports(ast: any, page: string): void {
341341
hasProxyExport = true
342342
}
343343
}
344-
if (
345-
node.type === 'ExportDeclaration' &&
346-
node.declaration?.type === 'VariableDeclaration'
347-
) {
348-
const declaration = node.declaration?.declarations[0]
349-
if (declaration?.id.value === 'middleware') {
350-
hasMiddlewareExport = true
351-
}
352-
if (declaration?.id.value === 'proxy') {
353-
hasProxyExport = true
354-
}
355-
}
356344
if (node.type === 'ExportNamedDeclaration') {
357345
for (const specifier of node.specifiers) {
358346
if (
@@ -378,7 +366,16 @@ function validateMiddlewareProxyExports(ast: any, page: string): void {
378366

379367
if (!hasValidExport) {
380368
throw new Error(
381-
`The ${fileName === 'proxy' ? 'Proxy' : 'Middleware'} "${page}" must export a \`${fileName}\` or a \`default\` function`
369+
`The file ${fileName} must export a function, either as a default export or as a named ${fileName} export.\n` +
370+
`This function is what Next.js runs for every request handled by this ${fileName === 'proxy' ? 'proxy (previously called middleware)' : 'middleware'}.\n\n` +
371+
`Why this happens:\n` +
372+
`- The file exists but doesn't export a function.\n` +
373+
`- The export is not a function (e.g., an object or constant).\n` +
374+
`- There's a syntax error preventing the export from being recognized.\n\n` +
375+
`To fix it:\n` +
376+
`- Check your "${fileName}" file.\n` +
377+
`- Ensure it has either a default or "${fileName}" function export.\n` +
378+
`- Restart the dev server if the error persists.`
382379
)
383380
}
384381
}

packages/next/src/build/templates/middleware.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@ const isProxy = page === '/proxy' || page === '/src/proxy'
1717
const handler = (isProxy ? mod.proxy : mod.middleware) || mod.default
1818

1919
if (typeof handler !== 'function') {
20+
const fileName = isProxy ? 'proxy' : 'middleware'
21+
2022
throw new Error(
21-
`The ${isProxy ? 'Proxy' : 'Middleware'} "${page}" must export a ${isProxy ? '`proxy`' : '`middleware`'} or a \`default\` function`
23+
`The file ${fileName} must export a function, either as a default export or as a named ${fileName} export.
24+
This function is what Next.js runs for every request handled by this ${fileName === 'proxy' ? 'proxy (previously called middleware)' : 'middleware'}.
25+
26+
Why this happens:
27+
- The file exists but doesn't export a function.
28+
- The export is not a function (e.g., an object or constant).
29+
- There's a syntax error preventing the export from being recognized.
30+
31+
To fix it:
32+
- Check your "${fileName}" file.
33+
- Ensure it has either a default or "${fileName}" function export.
34+
- Restart the dev server if the error persists.`
2235
)
2336
}
2437

0 commit comments

Comments
 (0)