How do you configure path aliases and the base path in Vite?
IntermediateBuild tools: webpack and Vite interview questionBy UIRound, updated 21 November 2026
Short answer
Path aliases go in two places that must agree: resolve.alias in vite.config.ts tells the bundler where @/ points, and paths in tsconfig.json tells TypeScript and your editor the same thing. The base path is the base option, which sets the public URL prefix for every built asset when the app is not served from the domain root. Your code reads it as import.meta.env.BASE_URL, and the router needs it as its basename.
Path aliases with resolve.alias
An alias replaces the start of an import specifier before Vite resolves it, so import { api } from "@/lib/api" works from any folder instead of ../../../lib/api.
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { fileURLToPath, URL } from "node:url";
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
});Use absolute paths as replacement values. Vite uses a relative value such as "./src" as it is, without resolving it against the config file, so @/lib/api becomes ./src/lib/api relative to whichever file imported it, and breaks as soon as that file is in a nested folder. fileURLToPath(new URL(..., import.meta.url)) works in an ES module config; path.resolve(__dirname, "src") works in a CommonJS one.
The alias option also accepts an array of { find, replacement } objects, where find can be a regular expression. Use that form when order matters (entries are tried in order, so put the more specific one first) or when a plain prefix is not precise enough:
resolve: {
alias: [{ find: /^@\//, replacement: fileURLToPath(new URL("./src/", import.meta.url)) }],
},How an alias fits into resolution alongside node_modules lookups and package exports is what implementing bundler module resolution asks you to build.
Make TypeScript agree
Vite does not read tsconfig.json paths for resolution on its own. TypeScript, in turn, does not read vite.config.ts. If you add only the Vite alias, the build works but the editor shows "Cannot find module" errors; if you add only the tsconfig paths, the editor is happy but the build fails.
// tsconfig.app.json (or tsconfig.json)
{
"compilerOptions": {
"paths": { "@/*": ["./src/*"] }
}
}The paths entries resolve relative to the tsconfig file. Older setups also set baseUrl, which paths has not needed for several TypeScript releases. Keep the two patterns identical in shape: @/* in TypeScript corresponds to the @ (or /^@\//) alias in Vite.
Watch which file you edit. The current Vite templates split config into tsconfig.json, which only references others, plus tsconfig.app.json for src and tsconfig.node.json for the config file. paths belongs in the file that includes src.
If you prefer one source of truth, the community vite-tsconfig-paths plugin reads the tsconfig paths and applies them as aliases, so you maintain them once.
Other tools need the mapping too. Vitest uses your Vite config, so it picks up resolve.alias automatically. Jest needs moduleNameMapper, and ESLint import rules need a TypeScript resolver. Jest vs Vitest covers why Vitest avoids this duplication.
The base path for subpath deploys
By default Vite builds for base: "/", so the HTML references /assets/index-4f9a2c1b.js. That breaks when the app lives under a subpath such as https://example.com/dashboard/ or a GitHub Pages project site, because the browser looks for /assets/... at the domain root.
export default defineConfig({
base: "/dashboard/",
});You can also set it per build with vite build --base=/dashboard/. Include the leading and trailing slash. Other accepted forms are a full URL, such as https://cdn.example.com/app/ when assets are served from a CDN, and "./" for relative URLs.
What base rewrites for you:
- script, stylesheet and asset URLs in the built
index.html - URLs of assets you import in JavaScript, such as
import logo from "./logo.svg" url()references in CSS- the dev server, which serves the app at
http://localhost:5173/dashboard/
What it cannot rewrite is a string you build yourself at runtime, such as a path to a file in public/. Use import.meta.env.BASE_URL for those:
<img src={`${import.meta.env.BASE_URL}images/hero.webp`} alt="" />
fetch(`${import.meta.env.BASE_URL}config.json`);BASE_URL is replaced with the literal value at build time, like other import.meta.env values. Loading config.json at runtime is a pattern in its own right: see loading runtime config instead of build-time env vars.
A relative base: "./" makes the build portable, but it only works reliably when the page URL stays in one folder. With history routing on a nested path like /dashboard/users/42, relative asset URLs resolve against /dashboard/users/ and fail. Use an absolute base for apps with client-side routes.
Tell the router about the base
The router has to know that /dashboard/ is not part of your routes, or every route will look unmatched. Pass the same value as the basename:
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const router = createBrowserRouter(routes, {
basename: import.meta.env.BASE_URL,
});
export const App = () => <RouterProvider router={router} />;Using BASE_URL instead of repeating the string keeps Vite and the router in sync. The server also needs a history fallback to the subfolder's index.html, which is covered in why a deployed SPA returns 404 on refresh. Micro-frontends use the same idea at a larger scale, with each app routing under its own base; see routing in micro-frontends.
Common follow-up questions
Why does my alias work in the build but not in the editor?
The tsconfig paths are missing, or they are in a tsconfig file that does not include your source folder. Restart the TypeScript server after changing them.
Do aliases change the output bundle?
No. They are resolved at build time, and the output contains bundled modules, not import paths. They only change how you write imports.
Can I change the base without rebuilding?
Not with base, because the prefix is written into the HTML and the asset URLs. If the same build has to run under different paths, use a relative base with hash routing, or serve the app from the same path everywhere.
How do I deploy to GitHub Pages?
A project site lives at https://user.github.io/repo/, so set base: "/repo/" and use the same value as the router basename. GitHub Pages has no rewrites, so deep links need hash routing or a 404.html copy of index.html.
Mistakes to avoid
Defining an alias in Vite but not in tsconfig, or the other way round
Using a relative path as an alias replacement value
Alias names like
utilsorcomponentsthat can shadow a real npm package of the same nameA prefix such as
@/or~/cannot collide.Deploying under a subpath with the default
base: "/"Hard-coding
/images/...for files inpublic/instead of prefixingBASE_URLForgetting the router
basename, so every route renders the not-found page
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Implement bundler module resolution: extensions,
node_modules, packageexportsconditions and aliasesHard60 min - Implement a basic client-side router using the History APIMedium45 min
- Migrate a large webpack app to Vite or Next.js without a feature freezeHard50 min
- Load runtime config from
/config.instead of build-time env varsMedium35 minjson