skip to content

In a Vite 8 project whose tsconfig.json maps `@/*` to `src/*`, why do `@/` imports type-check yet fail to resolve, and what fixes it?

level: middleimportance: must knowfreq 16%

answer

  1. paths informs the type checker only
  2. resolve.tsconfigPaths defaults to false
  3. alias replacement must be absolute
  4. tsconfig include decides which files
  5. two places to keep in sync

basics

~10 s

TypeScript's paths only inform the type checker, and Vite 8 ignores them by default. Either declare resolve.alias with an absolute replacement, or set resolve.tsconfigPaths: true so Vite applies the tsconfig paths itself.

solid answer

~40 s

A `paths` entry tells the type checker where `@/jobs/list` points; it rewrites nothing, and Vite 8 ignores it unless `resolve.tsconfigPaths` is `true` - the default is `false`. Two fixes. `resolve.alias`, for example `{ '@': fileURLToPath(new URL('./src', import.meta.url)) }`, is explicit and needs no tsconfig lookup, but duplicates the mapping; its replacement must be absolute, because a relative value is used as-is, and entries apply in order. `resolve.tsconfigPaths: true`, built in since Vite 8, keeps one source of truth at a small performance cost; it applies only to files a tsconfig matches through `files` or `include` (a stylesheet needs its extension listed) and not inside Less files. The default config loader ignores tsconfig settings, so `vite.config.ts` itself should import with relative paths.

code

typescript · 14 lines
typescript
// vite.config.ts (Vite 8)
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'

export default defineConfig({
  resolve: {
    // '@/jobs/list' -> <project>/src/jobs/list; the replacement must be absolute
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
    // apply tsconfig.json "paths", e.g. @jobboard/ui -> ../../packages/ui/src
    tsconfigPaths: true,
  },
})

go deeper

for a junior

Recall that tsconfig paths only satisfy the type checker and that Vite needs resolve.alias, or resolve.tsconfigPaths in Vite 8, to resolve those imports.

for a middle

Explain the object and array alias forms, why the replacement must be absolute, and which files resolve.tsconfigPaths applies to.

for a senior

Show you choose one source of truth per specifier, handle stylesheets and Less explicitly, and use dedupe when a linked package pulls a second framework copy.

for a principal

Weigh aliases against package.json imports and exports across a monorepo, so the build, the editor, tests and the runtime all agree on one mapping.

## Why the type checker and the build disagree A `tsconfig.json` entry such as `"paths": { "@/*": ["src/*"] }` tells the **type checker** where `@/jobs/list` points. It rewrites nothing in the emitted code - that is TypeScript's own rule - so whatever bundles the app has to apply the same mapping. In Vite 8 that is Vite, and by default it does **not** read `paths`: `resolve.tsconfigPaths` defaults to `false`. The editor is happy, a type-check passes, and the dev server reports that it failed to resolve the import. There are two fixes, and one project may use both for different specifiers. ## Fix 1: `resolve.alias` `resolve.alias` replaces an import specifier before Vite resolves it. It takes two shapes: | Form | Example | Use when | |---|---|---| | Object | `{ '@': '/abs/path/src' }` | simple prefix rewrites | | Array | `[{ find: /^@\//, replacement: '/abs/path/src/' }]` | a regular expression, `$1`-style capture groups, explicit order | Rules that bite: - **Absolute replacements only.** A relative value such as `'./src'` is used as-is and is not resolved to a file-system path. Build it with `fileURLToPath(new URL('./src', import.meta.url))`. - **Order matters.** The first-defined entries are applied first, so put the more specific ones earlier. - **Stylesheets too.** Vite respects aliases in CSS `@import` and in Sass and Less imports. - **Not the config file.** An alias rewrites application imports. The default config loader also ignores tsconfig settings when it bundles `vite.config.ts`, so the config should import its own helpers with relative paths. The cost of this fix is duplication: the mapping lives in `tsconfig.json` for types and in `vite.config.ts` for the build, and the two can drift. ## Fix 2: `resolve.tsconfigPaths` (Vite 8) Vite 8 can apply `paths` itself. Set `resolve.tsconfigPaths: true` and the tsconfig becomes the single source of truth - convenient for a shared package such as `@jobboard/ui` mapped to `packages/ui/src`. The details: - It has a **small performance cost**, which is why it is off by default. - `paths` applies only to a file **matched by a tsconfig** through its `files` or `include`. Vite uses the closest matching `tsconfig.json` for each file and follows that config's `references`, so a package with its own tsconfig keeps its own mapping. - A bare `"src"` include matches only TS and JS extensions. For `@import '@/styles/base.css'` to resolve, add `"src/**/*.css"` (or `.scss`) to `include`. - It does **not** apply inside `.less` files; use a relative path or an alias there. - Projects still carrying the community `vite-tsconfig-paths` plugin get a Vite 8 warning suggesting the built-in option instead. ## Choosing between them | | `resolve.alias` | `resolve.tsconfigPaths` | |---|---|---| | Source of truth | the Vite config, repeated in tsconfig for types | tsconfig only | | Cost | no tsconfig lookup | a small resolution cost | | Less files | works | not supported | | Non-JS files | works | must be listed in `include` | ## Diagnosing an import that will not resolve 1. **Find who should map it.** Is the specifier covered by a `resolve.alias` entry, by tsconfig `paths` with `resolve.tsconfigPaths: true`, or by neither? 2. **Check the alias value.** Log the replacement in the config; a relative or mistyped path is the most common fault. 3. **Check the importing file.** With `tsconfigPaths`, the file that contains the import must be matched by a tsconfig's `files` or `include`, and must not be a Less file. 4. **Check the order.** An earlier, broader alias entry may rewrite the specifier before the intended one sees it. 5. **Check the config file.** If the failing import is in `vite.config.ts` itself, neither mechanism applies; use a relative path. ## The top-level `tsconfig` option (Vite 8.3) Vite 8.3 added a top-level `tsconfig` option that names one tsconfig file for Vite to use. The docs discourage it: it overrides Vite's per-file discovery, which is aligned with the TypeScript language server. Put a `tsconfig.json` near the files it configures and use `references` for multi-project setups; to remap imports, prefer `resolve.alias` or the `imports` and `exports` fields of `package.json` over choosing a tsconfig for its `paths`. ## Neighbouring `resolve` keys, one line each - `resolve.dedupe` forces the listed packages to resolve to the single copy at the project root. - `resolve.extensions` lists the extensions tried for extensionless imports: `.mjs`, `.js`, `.mts`, `.ts`, `.jsx`, `.tsx`, `.json` by default. - `resolve.conditions` and `resolve.mainFields` steer which entry of a package is chosen; how export conditions work is a resolution subject of its own.

  • A shared component package reached through tsconfig paths has its own `node_modules` copy of the UI framework; how does `resolve.dedupe` help?
    Imports from files inside `packages/ui` resolve from that package's own `node_modules` first, so the app can end up with two copies of the framework and two instances of its state. Listing the framework in `resolve.dedupe` makes Vite resolve it to the single copy at the project root, whichever file imports it.
  • How do you alias one bare package name in Vite without also rewriting its subpath imports?
    Use the array form with a regular expression anchored at both ends, `{ find: /^chart-kit$/, replacement: '<absolute path to the file you want>' }`, so only the exact specifier matches and `chart-kit/theme` goes through normal resolution. With a regex `find`, the replacement may also use capture groups such as `$1`.
  • Why do Vite's docs discourage the top-level `tsconfig` option added in Vite 8.3?
    It replaces Vite's per-file tsconfig discovery, which matches what the TypeScript language server does, with one fixed file, so the editor and the build can disagree. The docs recommend a `tsconfig.json` near the files it configures plus `references`, and `resolve.alias` or `package.json` `imports` / `exports` when the goal is remapping imports.

TypeScript's paths are an entry in your own address book: you know where '@' lives, but the courier delivering your letter keeps a separate route list. Until you give the courier the address (resolve.alias) or let it read your address book (resolve.tsconfigPaths), the letter comes back undeliverable.

  • Vite reads tsconfig paths automatically, so an alias is never needed
  • A relative alias such as './src' is resolved from the config file
  • resolve.tsconfigPaths applies to every file, Less and unlisted CSS included
  • Vite 8 still needs a third-party plugin for tsconfig paths
  • The top-level tsconfig option is the recommended way to pick paths
  • An alias in resolve.alias also rewrites imports inside vite.config.ts

Assumes Vite 8.3; resolve.tsconfigPaths is built in since Vite 8.0 (earlier majors needed a plugin), and the top-level tsconfig option arrived in 8.3.