@tanstack/router-cli) provides command-line tools for generating route files and watching for changes in your routing structure.
Installation
Install the CLI as a development dependency:Configuration
Create atsr.config.json file in your project root to configure the router generator:
Configuration Options
string
default:"./src/routes"
The directory where your route files are located
string
default:"./src/routeTree.gen.ts"
The path where the generated route tree file will be created
string
Optional prefix for route files (e.g., “route” would match “route.home.tsx”)
string
default:"-"
Files starting with this prefix will be ignored by the route generator
string
A regex pattern to match files that should be ignored
'single' | 'double'
default:"single"
The quote style to use in generated files
boolean
default:false
Whether to include semicolons in generated files
boolean
default:false
Disable TypeScript type generation (outputs .js files instead)
boolean
default:false
Disable logging output from the CLI
boolean | string
default:false
Add file extensions to imports in generated files. Can be
true for .js or a custom extension like .tsstring[]
Custom headers to add to the generated route tree fileDefault:
boolean
Automatically enable code splitting for your routes
string | RegExp | object
default:"index"
The token to use for index routes (e.g., “index.tsx” matches index routes)
string | RegExp | object
default:"route"
The token to use for route files (e.g., “route.tsx” for file-based routing)
CLI Commands
The CLI provides two main commands:generate
Generate the route tree once:- Reads your route files from the configured
routesDirectory - Generates a type-safe route tree file at the configured
generatedRouteTreepath - Creates TypeScript types for all routes, search params, and path params
watch
Continuously watch for changes and regenerate routes:- Runs the generator initially
- Watches your
routesDirectoryfor changes - Automatically regenerates the route tree whenever files are added, modified, or deleted
- Keeps running until manually stopped
Usage in Scripts
Add the CLI commands to yourpackage.json scripts:
Generated Route Tree
The CLI generates a route tree file that looks like this:- Is fully type-safe
- Should not be edited manually (regenerated on changes)
- Exports a
routeTreeyou can pass tocreateRouter() - Can be safely committed to version control
Working Without the CLI
While the CLI is the recommended approach, you can also use the bundler plugins instead:- Vite Plugin - For Vite projects
- Bundler Plugins - For Webpack, ESBuild, or Rspack
Requirements
- Node.js >= 20.19
- A TypeScript or JavaScript project
- File-based routing structure in your project
Troubleshooting
Routes not generating
- Check that your
routesDirectorypath is correct intsr.config.json - Ensure route files follow the naming convention (e.g.,
route.tsxor use customrouteToken) - Verify the CLI has permission to write to the
generatedRouteTreelocation
Type errors after generation
- Restart your TypeScript server in your IDE
- Run
tsc --noEmitto check for type errors - Ensure the generated file is not excluded in your
tsconfig.json
Watch mode not detecting changes
- Ensure the process is still running
- Try stopping and restarting the watch command
- Check that your file system supports file watching