Skip to content
ESLint

ESLint Plugin Router

TanStack Router comes with its own ESLint plugin. This plugin is used to enforce best practices and to help you avoid common mistakes.

Installation

The plugin is a separate package that you need to install:

shell
npm i -D @tanstack/eslint-plugin-router

Flat Config (eslint.config.js)

The 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:

eslint.config.js
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):

eslint.config.js
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:

eslint.config.js
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.

Custom Flat Config setup

Alternatively, you can load the plugin and configure only the rules you want to use:

eslint.config.js
import pluginRouter from '@tanstack/eslint-plugin-router'

export default [
  {
    plugins: {
      '@tanstack/router': pluginRouter,
    },
    rules: {
      '@tanstack/router/create-route-property-order': 'error',
    },
  },
  // Any other config...
]

Legacy Config (.eslintrc)

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:

json
{
  "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:

json
{
  "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:

json
{
  "extends": [
    "plugin:@tanstack/eslint-plugin-router/react-recommended-type-checked"
  ]
}

Custom Legacy Config setup

Alternatively, add @tanstack/eslint-plugin-router to the plugins section, and configure the rules you want to use:

json
{
  "plugins": ["@tanstack/eslint-plugin-router"],
  "rules": {
    "@tanstack/router/create-route-property-order": "error"
  }
}

Rules

The following rules are available in the TanStack Router ESLint Plugin:

Conflicts with other ESLint plugins

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.

typescript-eslint

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.

json
{
  "rules": {
    "@typescript-eslint/only-throw-error": [
      "error",
      {
        "allow": [
          {
            "from": "package",
            "package": "@tanstack/router-core",
            "name": "Redirect"
          },
          {
            "from": "package",
            "package": "@tanstack/router-core",
            "name": "NotFoundError"
          }
        ]
      }
    ]
  }
}