Skip to content

Commit 4d1d748

Browse files
committed
Improve Proxy invalid export error message
1 parent 68b0225 commit 4d1d748

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
@@ -378,7 +378,16 @@ function validateMiddlewareProxyExports(ast: any, page: string): void {
378378

379379
if (!hasValidExport) {
380380
throw new Error(
381-
`The ${fileName === 'proxy' ? 'Proxy' : 'Middleware'} "${page}" must export a \`${fileName}\` or a \`default\` function`
381+
`The file ${fileName} must export a function, either as a default export or as a named ${fileName} export.\n` +
382+
`This function is what Next.js runs for every request handled by this ${fileName === 'proxy' ? 'proxy (previously called middleware)' : 'middleware'}.\n\n` +
383+
`Why this happens:\n` +
384+
`- The file exists but doesn't export a function.\n` +
385+
`- The export is not a function (e.g., an object or constant).\n` +
386+
`- There's a syntax error preventing the export from being recognized.\n\n` +
387+
`To fix it:\n` +
388+
`- Check your "${fileName}" file.\n` +
389+
`- Ensure it has either a default or "${fileName}" function export.\n` +
390+
`- Restart the dev server if the error persists.`
382391
)
383392
}
384393
}

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)