remix-run/react-router · error · Error
Could not find the routes directory: ${routesDir}. Did you f
Error message
Could not find the routes directory: ${routesDir}. Did you forget to create it? What it means
Thrown by flatRoutes() right after the root-route check: fs.existsSync(routesDir) is false, where routesDir = path.join(appDirectory, prefix) (prefix defaults to 'routes'). File-system routing requires the routes directory to exist, even if empty, because flatRoutes reads it with readdirSync.
Source
Thrown at packages/react-router-fs-routes/flatRoutes.ts:93
appDirectory: string,
ignoredFilePatterns: string[] = [],
prefix = "routes",
) {
let ignoredFileRegex = Array.from(new Set(["**/.*", ...ignoredFilePatterns]))
.map((re) => makeRe(re))
.filter((re: any): re is RegExp => !!re);
let routesDir = path.join(appDirectory, prefix);
let rootRoute = findFile(appDirectory, "root", routeModuleExts);
if (!rootRoute) {
throw new Error(
`Could not find a root route module in the app directory: ${appDirectory}`,
);
}
if (!fs.existsSync(routesDir)) {
throw new Error(
`Could not find the routes directory: ${routesDir}. Did you forget to create it?`,
);
}
// Only read the routes directory
let entries = fs.readdirSync(routesDir, {
withFileTypes: true,
encoding: "utf-8",
});
let routes: string[] = [];
for (let entry of entries) {
let filepath = normalizeSlashes(path.join(routesDir, entry.name));
let route: string | null = null;
// If it's a directory, don't recurse into it, instead just look for a route module
if (entry.isDirectory()) {
route = findRouteModuleForFolder(View on GitHub (pinned to 1fd704a7da)
Solutions
- Create the directory: `mkdir app/routes` (use the prefix path if customized).
- If you renamed the folder, either rename it back to `routes` or pass the new folder name as the `prefix` argument to flatRoutes().
- Add at least an index route (app/routes/_index.tsx) so the directory is non-empty and the app has a landing route.
- Re-run build/dev to confirm flatRoutes resolves.
Example fix
// before: app/root.tsx exists but app/routes/ is missing
// shell
mkdir -p app/routes
// optionally add app/routes/_index.tsx
// after: app/routes/_index.tsx
export default function Index() { return <h1>Home</h1>; } Defensive patterns
Strategy: validation
Validate before calling
import { existsSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
const routesDir = join(appDirectory, prefix ?? 'routes');
if (!existsSync(routesDir)) mkdirSync(routesDir, { recursive: true }); Type guard
function routesDirectoryExists(appDirectory: string, prefix = 'routes'): boolean {
const { existsSync } = require('node:fs');
return existsSync(`${appDirectory}/${prefix}`);
} Prevention
- Scaffold app/routes/ (or the configured prefix) in every new project.
- If renaming the folder, update the flatRoutes prefix argument.
- Add an index route file so the directory is non-empty.
When it happens
Trigger: flatRoutes() runs against an app directory that has root.tsx but no `routes/` subdirectory. Also triggered by a custom `prefix` (e.g. 'app/routes' set via the second argument) whose target directory doesn't exist.
Common situations: New project where routes/ wasn't scaffolded. Renaming routes/ to pages/ without updating the prefix. Custom flatRoutes(prefix) call pointing at a non-existent folder. Empty app where the developer intends to add routes later.
Related errors
- Could not find a root route module in the app directory: ${a
- Route segment "${segment}" for "${routeId}" cannot contain "
- React Router presets must have a `name` property defined.
- Could not find package.json in ${rootDirectory} or any of it
- Unable to define routes with duplicate route id: "${id}"
AI-assisted analysis of remix-run/react-router@1fd704a7da (2026-08-12).
Data as JSON: /api/errors/fbd8228a7555a53d.
Report an issue: GitHub.