remix-run/react-router · error

Could not find a root route module in the app directory

Error message

Could not find a root route module in the app directory: ${appDirectory}

What it means

flatRoutes() from @react-router/fs-routes derives the route tree from the filesystem and requires a root route module (root.tsx / root.jsx / root.mdx / etc., matched by routeModuleExts) directly inside the app directory. It looks the root file up before ever scanning routes/ and throws immediately when no match is found, since without a root the route tree has no top-level route to hang entries off.

Solutions

  1. Create app/root.tsx exporting a default root component (and usually a Layout export)
  2. Point the appDirectory config at the directory that actually contains your root module
  3. If you renamed the root on purpose, rename it back — flatRoutes hard-requires the name 'root'

Example fix

// before — react-router.config.ts (root.tsx actually lives in src/app/)
export default { appDirectory: "src" };

// after
export default { appDirectory: "src/app" };
// or simply create src/root.tsx
Defensive patterns

Strategy: validation

Validate before calling

import fs from "node:fs";
import path from "node:path";
const appDirectory = "app"; // same value passed to flatRoutes / config
const exts = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".mdx"];
const hasRoot = exts.some((e) => fs.existsSync(path.join(appDirectory, `root${e}`)));
if (!hasRoot) throw new Error(`Missing root route module in ${appDirectory}`);

Prevention

When it happens

Trigger: Calling flatRoutes() when appDirectory (default 'app') contains no file named root with any of the supported route module extensions — e.g. the root was renamed, lives in a subdirectory, or appDirectory was customized to a folder without it.

Common situations: Setting appDirectory: "src" while root.tsx still lives in "src/app"; renaming root.tsx to app.tsx; starting a project from a template that omits the root module.

Related errors


AI-assisted analysis of remix-run/react-router@6beaca3952 (2026-08-18). Data as JSON: /api/errors/c92164f789294d7a. Report an issue: GitHub.

Appendix: source

Thrown at packages/react-router-fs-routes/flatRoutes.ts:87

    return values;
  }
}

export function flatRoutes(
  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) {

View on GitHub (pinned to 6beaca3952)