Open History to see the last commit
1---
2id: path-aliases
3title: Path Aliases
4---
5β
6Path aliases are a useful feature of TypeScript that allows you to define a shortcut for a path that could be distant in your project's directory structure. This can help you avoid long relative imports in your code and make it easier to refactor your project's structure. This is especially useful for avoiding long relative imports in your code.
7β
8By default, TanStack Start does not include path aliases. However, you can easily add them to your project by updating your `tsconfig.json` file in the root of your project and adding the following configuration:
9β
10```json
11{
12 "compilerOptions": {
13 "baseUrl": ".",
14 "paths": {
15 "~/*": ["./src/*"]
16 }
17 }
18}
19```
20β
21In this example, we've defined the path alias `~/*` that maps to the `./src/*` directory. This means that you can now import files from the `src` directory using the `~` prefix.
22β
23After updating your `tsconfig.json` file, configure your build tool so it resolves the same path aliases.
24β
25<!-- ::start:tabs variant="bundler" -->
26β
27# Vite
28β
29## Vite 8
30β
31Vite 8+ has [built-in support for path aliases](https://vite.dev/config/shared-options#resolve-tsconfigpaths), which is disabled by default. To enable it, simply add the following configuration to your `vite.config.ts` file:
32β
33```ts
34// vite.config.ts
35import { defineConfig } from 'vite'
36β
37export default defineConfig({
38 resolve: {
39 // This enables built-in support for path aliases defined in tsconfig.json
40 tsconfigPaths: true,
41 },
42})
43```
44β
45## Vite 7 and earlier
46β
47For Vite 7 and earlier, install the `vite-tsconfig-paths` plugin to enable path aliases in your TanStack Start project. You can do this by running the following command:
48β
49```sh
50npm install -D vite-tsconfig-paths
51```
52β
53Now, you'll need to update your `vite.config.ts` file to include the following:
54β
55```ts
56// vite.config.ts
57import { defineConfig } from 'vite'
58import viteTsConfigPaths from 'vite-tsconfig-paths'
59β
60export default defineConfig({
61 plugins: [
62 // this is the plugin that enables path aliases
63 viteTsConfigPaths({
64 projects: ['./tsconfig.json'],
65 }),
66 ],
67})
68```
69β
70# Rsbuild
71β
72Rsbuild reads the `paths` field from `tsconfig.json` by default. No extra config is needed when your aliases live in the root `tsconfig.json`.
73β
74If you use a custom tsconfig file, point Rsbuild at it with `source.tsconfigPath`:
75β
76```ts
77// rsbuild.config.ts
78import { defineConfig } from '@rsbuild/core'
79β
80export default defineConfig({
81 source: {
82 tsconfigPath: './tsconfig.custom.json',
83 },
84})
85```
86β
87<!-- ::end:tabs -->
88β
89Once this configuration has completed, you'll now be able to import files using the path alias like so:
90β
91```ts
92// app/routes/posts/$postId/edit.tsx
93import { Input } from '~/components/ui/input'
94β
95// instead of
96β
97import { Input } from '../../../components/ui/input'
98```