Skip to content

Commit 380c842

Browse files
committed
Improve Proxy invalid export error message
1 parent f19dbfb commit 380c842

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,16 @@ function validateMiddlewareProxyExports({
392392

393393
if (!hasValidExport) {
394394
throw new Error(
395-
`The ${fileName === 'proxy' ? 'Proxy' : 'Middleware'} file "./${basename(pageFilePath)}" must export a function named \`${fileName}\` or a default function.`
395+
`The ${fileName === 'proxy' ? 'Proxy' : 'Middleware'} file "./${basename(pageFilePath)}" must export a function named \`${fileName}\` or a default function.\n` +
396+
`This function is what Next.js runs for every request handled by this ${fileName === 'proxy' ? 'proxy (previously called middleware)' : 'middleware'}.\n\n` +
397+
`Why this happens:\n` +
398+
`- The file exists but doesn't export a function.\n` +
399+
`- The export is not a function (e.g., an object or constant).\n` +
400+
`- There's a syntax error preventing the export from being recognized.\n\n` +
401+
`To fix it:\n` +
402+
`- Check your "${fileName}" file.\n` +
403+
`- Ensure it has either a default or "${fileName}" function export.\n` +
404+
`- Restart the dev server if the error persists.`
396405
)
397406
}
398407
}

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

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

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

0 commit comments

Comments
 (0)