Configure TypeScript paths in Vite and Vitest
Make TypeScript path aliases work in Vite and Vitest by repeating the alias in resolve.alias, or reading it from tsconfig with a plugin.
TypeScript's paths option only helps the type checker. It does not rewrite imports, so Vite and Vitest need the alias too.
{
"compilerOptions": {
"baseUrl": ".",
"paths": { "@/*": ["src/*"] }
}
}import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';
export default defineConfig({
resolve: {
alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) },
},
test: { environment: 'node' },
});import { formatPrice } from '@/lib/format';Vitest reads vite.config.js, so one resolve.alias entry serves the dev server, the build and the tests. Without it, the editor is happy and the run fails with Failed to resolve import "@/lib/format". Keeping two lists in step is the weak point; the vite-tsconfig-paths plugin reads tsconfig.json so the list lives in one file. See [the paths option](https://www.typescriptlang.org/tsconfig/#paths).
Use the test key only when you import defineConfig from vitest/config, which types it. The plain vite import still runs it but flags a type error.