TanStack Router comes with its own ESLint plugin. This plugin is used to enforce best practices and to help you avoid common mistakes.
The plugin is a separate package that you need to install:
npm i -D @tanstack/eslint-plugin-routerpnpm add -D @tanstack/eslint-plugin-routeryarn add -D @tanstack/eslint-plugin-routerbun add -d @tanstack/eslint-plugin-routerThe release of ESLint 9.0 introduced a new way to configure ESLint using a flat config format. This new format is more flexible and allows you to configure ESLint in a more granular way than the legacy .eslintrc format. The TanStack Router ESLint Plugin supports this new format and provides a recommended config that you can use to enable all of the recommended rules for the plugin .
To enable the framework-agnostic recommended rules for our plugin, add the following config:
import pluginRouter from '@tanstack/eslint-plugin-router'
export default [
...pluginRouter.configs['flat/recommended'],
// Any other config...
]If your project uses @tanstack/react-router, use flat/react-recommended to enable all framework-agnostic recommended rules along with React-only rules that prevent unnecessary re-renders in React (useSyncExternalStore subscriptions):
import pluginRouter from '@tanstack/eslint-plugin-router'
export default [
...pluginRouter.configs['flat/react-recommended'],
// Any other config...
]These additional React rules only inspect imports from @tanstack/react-router. Solid and Vue adapters do not have the same React useSyncExternalStore re-render hazard, so projects using @tanstack/solid-router or @tanstack/vue-router should use flat/recommended.
If your project uses @tanstack/react-router with type-aware ESLint (parserOptions.projectService: true), use flat/react-recommended-type-checked to enable all rules from flat/react-recommended plus type-checked rules such as @tanstack/router/prefer-structural-sharing:
import tseslint from 'typescript-eslint'
import pluginRouter from '@tanstack/eslint-plugin-router'
export default [
...tseslint.configs.recommendedTypeChecked,
...pluginRouter.configs['flat/react-recommended-type-checked'],
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
files: ['**/*.{js,mjs,cjs}'],
...tseslint.configs.disableTypeChecked,
rules: {
...tseslint.configs.disableTypeChecked.rules,
'@tanstack/router/prefer-structural-sharing': 'off',
},
},
// Any other config...
]The non-type-checked flat/recommended and flat/react-recommended configs do not require TypeScript parser type information.
Alternatively, you can load the plugin and configure only the rules you want to use:
import pluginRouter from '@tanstack/eslint-plugin-router'
export default [
{
plugins: {
'@tanstack/router': pluginRouter,
},
rules: {
'@tanstack/router/create-route-property-order': 'error',
},
},
// Any other config...
]Prior to the ESLint 9.0 release, the most common way of configuring EsLint was using a .eslintrc file. The TanStack Router ESLint Plugin still supports this configuration method.
To enable the framework-agnostic recommended rules for our plugin, add plugin:@tanstack/eslint-plugin-router/recommended in extends:
{
"extends": ["plugin:@tanstack/eslint-plugin-router/recommended"]
}For projects using @tanstack/react-router, use plugin:@tanstack/eslint-plugin-router/react-recommended instead to include both the framework-agnostic recommended rules and the React-only rules:
{
"extends": ["plugin:@tanstack/eslint-plugin-router/react-recommended"]
}For projects using @tanstack/react-router with type-aware @typescript-eslint/parser configuration, use plugin:@tanstack/eslint-plugin-router/react-recommended-type-checked:
{
"extends": [
"plugin:@tanstack/eslint-plugin-router/react-recommended-type-checked"
]
}Alternatively, add @tanstack/eslint-plugin-router to the plugins section, and configure the rules you want to use:
{
"plugins": ["@tanstack/eslint-plugin-router"],
"rules": {
"@tanstack/router/create-route-property-order": "error"
}
}The following rules are available in the TanStack Router ESLint Plugin:
If you have other ESLint plugins installed, they may rules that conflict with this plugin. If so, you'll need to make some tweaks to allow these plugins to work together.
The @typescript-eslint/only-throw-error rule, enabled by default in the recommended-type-checked and strict-type-checked rulesets, disallows the throwing of non-Error values as exceptions, which is considered a good practice.
To ensure it does not conflict with TanStack Router, you should allow redirect and notFound as throwable objects.
{
"rules": {
"@typescript-eslint/only-throw-error": [
"error",
{
"allow": [
{
"from": "package",
"package": "@tanstack/router-core",
"name": "Redirect"
},
{
"from": "package",
"package": "@tanstack/router-core",
"name": "NotFoundError"
}
]
}
]
}
}