Skip to main content

CLI

The TanStack Router CLI (tsr) provides command-line tools for generating and managing your routes without needing a bundler plugin.

Installation

Or install globally:

Commands

The CLI provides two main commands:

generate

Generates the route tree for your project once.
This command:
  1. Reads your configuration from tsr.config.json
  2. Scans your routes directory
  3. Generates the route tree file
  4. Exits when complete

watch

Continuously watches for route changes and regenerates the route tree automatically.
This command:
  1. Reads your configuration from tsr.config.json
  2. Scans your routes directory
  3. Generates the route tree file
  4. Watches for file changes
  5. Regenerates the route tree when routes are added, modified, or deleted
  6. Keeps running until you stop it

Configuration

Create a tsr.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 .js file 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 your package.json:
Then run:

Usage Patterns

Development Workflow

  1. Start the CLI watcher:
  2. In another terminal, start your dev server:
  3. Add/modify routes in your routes directory
  4. The CLI automatically regenerates the route tree
  5. Your dev server hot-reloads with the new routes

Build Workflow

Generate routes before building:
Or use a combined script:

CI/CD

In CI environments, use the generate command:

How It Works

The CLI uses the same route generation logic as the bundler plugins:
  1. Configuration: Reads tsr.config.json from your project root
  2. Scanning: Scans your routesDirectory for route files
  3. Generation: Generates a typed route tree file
  4. Watching (watch mode only): Uses chokidar to watch for file changes
  5. 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

If tsr is not found:
  1. Make sure @tanstack/router-cli is installed
  2. Use npx tsr instead of tsr
  3. Or add it to your package.json scripts

Configuration Not Found

The CLI looks for tsr.config.json in the current directory. Make sure:
  1. The file exists in your project root
  2. The JSON is valid (use a JSON validator)
  3. You’re running the command from the correct directory

Routes Not Generated

If routes aren’t being generated:
  1. Check that routesDirectory exists and has route files
  2. Verify the path is correct (relative to project root)
  3. Check file permissions
  4. Look for error messages in the CLI output

Watch Mode Not Detecting Changes

If watch mode isn’t detecting changes:
  1. Make sure you’re editing files inside routesDirectory
  2. Check that files don’t match routeFileIgnorePattern
  3. Restart the watch command
  4. On some systems, watch limits may need adjustment

Generated File Has Wrong Extension

If your generated file has the wrong extension:
  • Check disableTypes setting
  • When disableTypes: true, the CLI generates .js files
  • When disableTypes: false, the CLI generates .ts files

Advanced Usage

Programmatic Usage

You can use the CLI programmatically in your own scripts:
Or use the watch functionality:

Custom Config Location

While the CLI looks for tsr.config.json in the current directory, you can work around this by changing directories:
Or use programmatic usage with a custom config path.