How to Set Up an `@/` Path Alias for Imports in React with Vite
To write imports like import { PriceTag } from '@/components/ui/PriceTag' in a React app built with Vite, you add the @/ setting in both paths in tsconfig.app.json and resolve in vite.config.ts. When folders get deeper and relative paths like ../../components/… become long, this lets you import from src instead (this is called a path alias).
With only one of the two, either the type check (tsc) or the Vite dev server fails to find the file. In my test environment, I checked tsc -b, the dev server, and the production build for three cases: no setup, only one of the two, and both.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Vite 8.3.8, @vitejs/plugin-react 6.3.0, React 19.3.1, and TypeScript 7.0.2. I created the project with the react-ts template from npm create vite@latest. I opened the production build in Chromium from Playwright 1.63.0 to check the page. I did not test editor completion in VS Code or other editors, tests with Vitest, using @/ inside CSS, or Windows and macOS.
The file structure I tested
I created three levels of folders under src and imported everything with @/ (only the main files are shown).
src/
├── App.tsx
├── main.tsx
├── components/ui/PriceTag.tsx
├── lib/format.ts
└── pages/settings/PlanPage.tsx
// src/pages/settings/PlanPage.tsx
// With a relative path, this would be '../../components/ui/PriceTag'
import { PriceTag } from '@/components/ui/PriceTag'
export function PlanPage() {
return (
<section>
<h1>プランの設定</h1>
<p>
月額:<PriceTag yen={1200} />
</p>
</section>
)
}
App.tsx imports @/pages/settings/PlanPage and PriceTag.tsx imports @/lib/format in the same way.
With the template as it was and no setup, npx tsc -b reported TS2307 errors in the three files.
$ npx tsc -b
src/App.tsx(1,26): error TS2307: Cannot find module '@/pages/settings/PlanPage' or its corresponding type declarations.
src/components/ui/PriceTag.tsx(1,29): error TS2307: Cannot find module '@/lib/format' or its corresponding type declarations.
src/pages/settings/PlanPage.tsx(2,26): error TS2307: Cannot find module '@/components/ui/PriceTag' or its corresponding type declarations.
npx vite build also stopped with "Rolldown failed to resolve import "@/pages/settings/PlanPage"", and the dev server failed with the same unresolved import.
Add paths to tsconfig.app.json
The paths option under compilerOptions tells TypeScript what @/ means. In the react-ts template, the type-check settings for src are in tsconfig.app.json, so that is where I added it. (tsconfig.json only references the two config files.)
{
"compilerOptions": {
// …template settings unchanged…
"noFallthroughCasesInSwitch": true,
/* Path alias */
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"]
}
The part matched by * in @/* is inserted into the * in ./src/*. ./src/* is a relative path from the location of tsconfig.app.json. With this, npx tsc -b finished without errors.
However, as the official TypeScript reference explains, paths only tells TypeScript that the same mapping is used by other tools, such as bundlers, at runtime or during bundling. The import paths that tsc emits are not rewritten. Vite is the one that actually loads the files, so Vite also needs its own setting.
Point @ to src with resolve.alias in vite.config.ts
Vite's setting goes in resolve.alias.
// vite.config.ts
import { fileURLToPath } from 'node:url'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
})
The Vite documentation says to use an absolute path when an alias points to a file path. A relative path left as is is not resolved as a file path and is used literally. So I built the absolute path to ./src based on the location of vite.config.ts. The types for node:url came from @types/node, which is included in the template.
With both paths and this setting in place, the dev server returned /src/App.tsx and /src/pages/settings/PlanPage.tsx with HTTP status 200. npm run build (tsc -b && vite build) also passed, and the page opened with vite preview showed the plan settings heading and a monthly price of 1,200 yen.
resolve.tsconfigPaths keeps the setup in one place
Vite 8.3.8, the version I tested, also has resolve.tsconfigPaths, which makes Vite read the paths from tsconfig directly (the default is false). If you use this instead of resolve.alias, you only need to write the @/ mapping in the paths of tsconfig.app.json.
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
tsconfigPaths: true,
},
})
In my test environment, with this setup the dev server also returned the two files with status 200, and npm run build and the page shown by vite preview were the same as with resolve.alias.
However, the paths section of Vite's features page says this feature has a performance cost, and that the TypeScript team does not recommend using paths to change how external tools behave. If you want to avoid the performance cost, write the alias in resolve.alias and keep it in sync with the paths in tsconfig in two places.
Errors when only one side is set
These are the results when one of the two settings is missing.
| Setting | tsc -b |
Dev server, fetching /src/App.tsx |
vite build |
|---|---|---|---|
| None | TS2307 | 500 error | Error |
paths only |
Passes | 500 error | Passes |
resolve.alias only |
TS2307 | 200 | Not tested on its own (npm run build stops at tsc -b) |
paths and resolve.alias |
Passes | 200 | Passes |
With only paths, the browser opened in the dev server showed the following error.

Failed to resolve import "@/pages/settings/PlanPage" from "src/App.tsx". Does the file exist?
In my test environment, with only paths, npx vite build succeeded and the page shown by vite preview displayed correctly. The dev server still showed an error. Do not decide based only on whether the build passes. Also open the app with npm run dev to check.
Conversely, with only resolve.alias, the dev server returned /src/App.tsx with status 200, but npm run build stopped at the first tsc -b with TS2307 and exit code 1.
Pitfalls when writing paths
If you write the right-hand side of paths as "src/*" without the leading ./, TypeScript 7.0's tsc -b reports the following error.
tsconfig.app.json(27,15): error TS5090: Non-relative paths are not allowed. Did you forget a leading './'?
The TypeScript reference says that when baseUrl is set, paths are relative to it. However, adding "baseUrl": "." in TypeScript 7.0 produced the following error.
tsconfig.app.json(26,5): error TS5102: Option 'baseUrl' has been removed. Please remove it from your configuration.
Use '"paths": {"*": ["./*"]}' instead.
TypeScript 7.0 no longer supports baseUrl, so write the right-hand side of paths with a leading ./, such as ./src/*, and do not write baseUrl.





