How to Fix Next.js Middleware Error in AI-Built Apps
Next.js middleware throws errors or doesn't execute as expected. AI-generated middleware often uses Node.js APIs unavailable in the Edge runtime or is placed in the wrong directory.
Error messages you might see
Error: middleware must be placed in the root directory (src/middleware.ts)Dynamic Code Evaluation (e.g. 'eval', 'new Function') not allowed in Edge RuntimeWhy this happens in AI-generated code
Middleware in wrong location
AI places middleware.ts inside app/ or pages/ instead of the project root (or src/ root).
Using Node.js APIs in Edge Runtime
AI imports fs, path, crypto, or other Node.js modules that aren't available in the Edge runtime.
Infinite redirect loops
Middleware redirects to a path that also triggers the middleware, creating an infinite loop.
How to fix it
Place middleware in the correct location
Put `middleware.ts` in your project root or `src/` directory, at the same level as `app/` or `pages/`.
Use Edge-compatible APIs only
Replace Node.js crypto with `crypto.subtle`, avoid `fs` and `path`. Use the `matcher` config to limit which routes trigger middleware.
Get professional help
Our code audit identifies this issue and provides a fix. Start at springcode.co/products for $19.
Related technologies
Can't fix it yourself?
Our code audit identifies this issue and dozens more. Get a prioritized fix list.
Security Review
Automated Security Scan
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedSecurity Review
Manual Security Review
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteSecurity Review
Full Pentest
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Fix Bugs
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedFix Bugs
Bug Fixing
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteFix Bugs
Ongoing Support
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Refactor Code
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedRefactor Code
Refactoring
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteRefactor Code
Full Rewrite
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
100% of your audit purchase is credited toward any paid service. Start with an audit, then let us fix what we find.
Frequently asked questions
Can I use npm packages in middleware?
Only packages that don't rely on Node.js-specific APIs. Check if the package supports Edge runtime before importing.
How do I debug Next.js middleware?
Use `console.log` in middleware and check the terminal output. Middleware runs on the server, not in the browser DevTools.
Related resources
Related Technologies
Still stuck? We can fix it for you.
Send us your repo. We'll diagnose the issue and give you a fixed quote within 24 hours.