Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tanstack/router/llms.txt
Use this file to discover all available pages before exploring further.
CLI
The TanStack Router CLI (tsr) provides command-line tools for generating and managing your routes without needing a bundler plugin.
Installation
Commands
The CLI provides two main commands:generate
Generates the route tree for your project once.
- Reads your configuration from
tsr.config.json - Scans your routes directory
- Generates the route tree file
- Exits when complete
watch
Continuously watches for route changes and regenerates the route tree automatically.
- Reads your configuration from
tsr.config.json - Scans your routes directory
- Generates the route tree file
- Watches for file changes
- Regenerates the route tree when routes are added, modified, or deleted
- Keeps running until you stop it
Configuration
Create atsr.config.json file in your project root:
Configuration Options
Route Generation
routesDirectory
- Type:
string - Default:
'./src/routes' - The directory where your route files are located.
generatedRouteTree
- Type:
string - Default:
'./src/routeTree.gen.ts' - The file where the generated route tree will be written.
routeFilePrefix
- Type:
string - Optional
- A prefix to require for all route files.
routeFileIgnorePrefix
- Type:
string - Default:
'-' - Files starting with this prefix will be ignored during route generation.
routeFileIgnorePattern
- Type:
string - Optional
- A regex pattern to ignore specific route files.
Code Style
quoteStyle
- Type:
'single' | 'double' - Default:
'single' - Quote style for generated code.
semicolons
- Type:
boolean - Default:
false - Whether to include semicolons in generated code.
enableRouteTreeFormatting
- Type:
boolean - Default:
true - Format the generated route tree file with Prettier.
routeTreeFileHeader
- Type:
string[] - Default:
['/* eslint-disable */', '// @ts-nocheck', '// noinspection JSUnusedGlobalSymbols'] - Lines to add at the top of the generated route tree file.
TypeScript
disableTypes
- Type:
boolean - Default:
false - Disable TypeScript type generation. When
true, generates a.jsfile instead of.ts.
Advanced Options
target
- Type:
'react' | 'solid' | 'vue' - Default:
'react' - The framework target for route generation.
disableLogging
- Type:
boolean - Default:
false - Disable CLI logging output.
autoCodeSplitting
- Type:
boolean - Optional
- Enable automatic code splitting for generated routes.
addExtensions
- Type:
boolean | string - Default:
false - Add file extensions to route imports.
indexToken
- Type:
string | RegExp | { regex: string, flags?: string } - Default:
'index' - Token to identify index routes.
routeToken
- Type:
string | RegExp | { regex: string, flags?: string } - Default:
'route' - Token to identify route files.
Example Configurations
Basic Configuration
TypeScript Disabled
Custom File Patterns
Double Quotes and Semicolons
Solid.js Project
Custom Route Tokens
npm Scripts
Add CLI commands to yourpackage.json:
Usage Patterns
Development Workflow
-
Start the CLI watcher:
-
In another terminal, start your dev server:
- Add/modify routes in your routes directory
- The CLI automatically regenerates the route tree
- Your dev server hot-reloads with the new routes
Build Workflow
Generate routes before building:CI/CD
In CI environments, use thegenerate command:
How It Works
The CLI uses the same route generation logic as the bundler plugins:- Configuration: Reads
tsr.config.jsonfrom your project root - Scanning: Scans your
routesDirectoryfor route files - Generation: Generates a typed route tree file
- Watching (watch mode only): Uses
chokidarto watch for file changes - Regeneration: Automatically regenerates on file create/update/delete events
When to Use the CLI
Use the CLI when:
- You’re not using a bundler plugin
- You want explicit control over route generation
- You’re working in a monorepo and need independent route generation
- You’re integrating with custom build tools
- You prefer separate processes for route generation and bundling
Use a plugin instead when:
- You’re already using Vite, Webpack, ESBuild, or Rspack
- You want automatic integration with your bundler
- You need code splitting features
- You want zero-config setup
Troubleshooting
Command Not Found
Iftsr is not found:
- Make sure
@tanstack/router-cliis installed - Use
npx tsrinstead oftsr - Or add it to your
package.jsonscripts
Configuration Not Found
The CLI looks fortsr.config.json in the current directory. Make sure:
- The file exists in your project root
- The JSON is valid (use a JSON validator)
- You’re running the command from the correct directory
Routes Not Generated
If routes aren’t being generated:- Check that
routesDirectoryexists and has route files - Verify the path is correct (relative to project root)
- Check file permissions
- Look for error messages in the CLI output
Watch Mode Not Detecting Changes
If watch mode isn’t detecting changes:- Make sure you’re editing files inside
routesDirectory - Check that files don’t match
routeFileIgnorePattern - Restart the watch command
- On some systems, watch limits may need adjustment
Generated File Has Wrong Extension
If your generated file has the wrong extension:- Check
disableTypessetting - When
disableTypes: true, the CLI generates.jsfiles - When
disableTypes: false, the CLI generates.tsfiles
Advanced Usage
Programmatic Usage
You can use the CLI programmatically in your own scripts:Custom Config Location
While the CLI looks fortsr.config.json in the current directory, you can work around this by changing directories: