How to Fix: folder name ‘middleware’ conflicted with middleware.ts when deploy to vercel.
Vercel deployment fails because a folder named middleware conflicts with middleware.ts
If your app deploys locally but breaks on Vercel when you have both a middleware directory and a middleware.ts file, the problem is usually a route and file-system naming collision. In frameworks like Next.js, middleware.ts is a special reserved file. Giving a folder the same effective name can confuse build-time resolution, especially in Vercel’s production environment where framework conventions are enforced more strictly.
Table of Contents
Understanding the Root Cause
In Next.js, middleware.ts is not just a normal source file. It is a special framework entry point that Vercel and Next.js scan automatically during the build. Its purpose is to run logic before a request completes, such as authentication, redirects, rewrites, or header manipulation.
When you also create a folder named middleware, you introduce ambiguity into the project structure. Even if your local environment appears to tolerate it, Vercel’s production build can treat the naming as a conflict because:
- middleware.ts is reserved and loaded by convention.
- A sibling or nearby directory named middleware may be interpreted as a competing module path.
- Imports like
import x from './middleware'can become ambiguous depending on resolution order. - Case sensitivity and deployment file-system differences can expose issues that do not surface on macOS or Windows.
The result is often a build failure, incorrect module resolution, or middleware not being detected properly on Vercel.
Step-by-Step Solution
The safest fix is to rename the folder so only the reserved framework file keeps the name middleware.ts.
1. Keep the reserved file name
If you are using Next.js middleware, the file should remain named exactly:
middleware.ts
Example:
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
return NextResponse.next()
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}
2. Rename the folder named middleware
Change the conflicting directory to something descriptive such as:
- middleware-utils
- middleware-lib
- request-middleware
- edge-utils
For example, change this:
src/
middleware/
auth.ts
middleware.ts
To this:
src/
middleware-utils/
auth.ts
middleware.ts
3. Update imports
After renaming the folder, fix all affected imports.
import { validateAuth } from './middleware-utils/auth'
If you use path aliases in tsconfig.json or jsconfig.json, update those too.
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/middleware-utils/*": ["src/middleware-utils/*"]
}
}
}
4. Clear stale build artifacts
Old caches can preserve broken references. Remove local build output before testing again.
rm -rf .next
rm -rf node_modules/.cache
If needed, reinstall dependencies:
rm -rf node_modules package-lock.json
npm install
5. Commit the rename carefully
On case-insensitive file systems, renames are sometimes not detected correctly by Git. If the directory rename is subtle, force Git to register it.
git mv middleware middleware-utils
git commit -m "Rename middleware folder to avoid conflict with middleware.ts"
6. Redeploy to Vercel
Push the change and trigger a fresh deployment from Vercel. If your previous deployment cache was problematic, retry with cache cleared from the Vercel dashboard.
7. Recommended final structure
src/
middleware-utils/
auth.ts
headers.ts
middleware.ts
This structure keeps the special middleware entry file separate from your reusable helper modules.
Common Edge Cases
- Using the App Router and Pages Router together: Mixed project structures can make naming issues harder to spot. Verify there is only one intended middleware.ts at the supported root level.
- Case-only renames: Renaming
Middlewaretomiddlewareor similar may not be recognized properly on macOS or Windows. Usegit mvwith an intermediate name if necessary. - Import path shortcuts: Aliases like
@/middlewaremay still point to the old folder after renaming. Check tsconfig, webpack, or custom resolver settings. - Monorepo collisions: In a monorepo, another package may expose a module named middleware. Make sure local imports are explicit and not shadowed.
- Incorrect middleware location: In Next.js, the special file must be placed where the framework expects it. A valid name in the wrong location can still fail.
- Multiple middleware files: If your project accidentally contains more than one middleware.ts, Vercel may reject the build or use unexpected behavior.
FAQ
Can I keep both a folder named middleware and a middleware.ts file?
Technically, some local setups may appear to allow it, but it is not a safe structure for Vercel and Next.js. Because middleware.ts is reserved, the best practice is to rename the folder.
Why does it work locally but fail only on Vercel?
Local development can hide module resolution and file-system sensitivity issues. Vercel builds in a clean production environment where reserved file conventions are enforced more predictably.
What should I name the folder instead?
Use a purpose-driven name that does not collide with framework conventions, such as middleware-utils, edge-utils, or request-helpers.
The core fix is simple: reserve the name middleware.ts for the framework file only, and rename any folder or helper module that reuses the same name. Once the collision is removed, Vercel deployments usually succeed without any additional framework changes.