@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
The directory where your route files are located
The path where the generated route tree file will be created
Optional prefix for route files (e.g., “route” would match “route.home.tsx”)
Files starting with this prefix will be ignored by the route generator
A regex pattern to match files that should be ignored
The quote style to use in generated files
Whether to include semicolons in generated files
Disable TypeScript type generation (outputs .js files instead)
Disable logging output from the CLI
Add file extensions to imports in generated files. Can be
true for .js or a custom extension like .tsCustom headers to add to the generated route tree fileDefault:
Automatically enable code splitting for your routes
The token to use for index routes (e.g., “index.tsx” matches index routes)
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