🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Astro Path Alias Setup with tsconfig paths

Set up an Astro path alias like @/components in tsconfig.json so imports stop climbing ../../../, with editor support, MDX imports and common errors fixed.

#Astro#TypeScript#Path Aliases#Configuration

To add an Astro path alias, define it under compilerOptions.paths in tsconfig.json, for example "@/*": ["./src/*"]. Astro reads those paths and passes them to Vite, so import Card from '@/components/Card.astro' works in components, pages, MDX and scripts without extra config in astro.config.mjs.

Key Takeaways

  • Aliases live in tsconfig.json; Astro applies them to the build automatically.
  • One alias, @/* pointing at src/*, covers most projects.
  • Restart the dev server and your editor’s TypeScript server after changing paths.
  • Aliases do not apply to public/ files, which you reference by URL, not import.

Add the alias

Start from the tsconfig.json Astro created, which extends one of Astro’s presets:

{
  "extends": "astro/tsconfigs/strict",
  "include": [".astro/types.d.ts", "**/*"],
  "exclude": ["dist"],
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

Relative paths in paths resolve from the tsconfig.json location, so ./src/* works without a baseUrl. Now replace the climbing imports:

---
// src/pages/blog/[id].astro
// Before: import BaseLayout from '../../layouts/BaseLayout.astro';
import BaseLayout from '@/layouts/BaseLayout.astro';
import { formatDate } from '@/utils/date';
import heroImage from '@/assets/hero.jpg';
---

Moving a page to a deeper folder no longer breaks its imports, which matters when you reorganise routes.

Several aliases

Some teams prefer one alias per folder. That is fine, but keep the list short; every alias is something new contributors must learn:

{
  "compilerOptions": {
    "paths": {
      "@components/*": ["./src/components/*"],
      "@layouts/*": ["./src/layouts/*"],
      "@utils/*": ["./src/utils/*"],
      "@assets/*": ["./src/assets/*"]
    }
  }
}

Avoid an alias named exactly @ with no slash, and avoid prefixes that collide with npm scopes you install, such as @astrojs.

In MDX and Markdown content

MDX imports go through the same resolver, so aliases work there:

---
title: Pricing changes in November
---
import Callout from '@/components/Callout.astro';

<Callout type="info">The new plans apply from the first of the month.</Callout>

Image paths in Markdown frontmatter handled by the image() schema helper are resolved relative to the entry file, not through aliases, so keep those relative.

In tests and other tools

Tools that use Astro’s Vite config inherit the alias. Vitest configured with getViteConfig() from astro/config picks it up, as our Vitest tip shows. ESLint’s import rules may need a TypeScript resolver to understand @/; see the ESLint and Prettier tip.

When the alias does not resolve

If you see Cannot find module '@/components/Card.astro' or a Vite “Failed to resolve import” error:

  1. Check the key and value both end in /*.
  2. Make sure the path is relative to tsconfig.json, usually ./src/*.
  3. Restart astro dev; config is read at startup.
  4. Restart the TypeScript server in your editor, or run astro sync and astro check.
  5. Confirm there is only one tsconfig.json in play, especially in a monorepo.

Our TypeScript config guide explains the presets, and the VS Code setup guide covers editor support. The folder structure docs show where each folder lives in our Astro themes, and the official TypeScript guide documents aliases.

FAQ

How do I create an import alias in Astro?

Add "paths": { "@/*": ["./src/*"] } under compilerOptions in tsconfig.json. Astro applies it to imports in components, pages, MDX and scripts.

Do I need to configure Vite aliases in astro.config.mjs?

No. Astro reads paths from tsconfig.json and configures Vite for you. A separate vite.resolve.alias is only needed for tools that do not read tsconfig.json.

Do Astro path aliases work in JavaScript projects?

Yes. Use a jsconfig.json with the same compilerOptions.paths, or keep a tsconfig.json even if you write JavaScript.

Can I use an alias for files in public/?

No. Files in public/ are served by URL, not imported. Reference them as /favicon.svg, or move them to src/assets/ and import them through the alias.