Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

An import alias

1m:0s

One of the most important things that will make your life a lot simpler is to set up an import alias for your files.

My favorite import alias is set to the src folder because almost everything we need is in there. Then I can import components and layouts like this.

---
import Component from '@/components/basename.astro'
import Layout from '@/layouts/basename.astro'
---

Setting up your import alias

You need to set up your import alias in two places:

  1. In jsconfig.json (or tsconfig.json if you use TypeScript)
  2. In astro.config.mjs

The jsconfig.json makes IntelliSense work in your code editor so it is pretty important. It looks like this:

jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}

Just having this in jsconfig.json is not enough because Vite (the build tool behind Astro) needs to know the alias as well. So you need to set up the alias in astro.config.mjs as well.

It looks like this:

astro.config.mjs
import { URL, fileURLToPath } from 'node:url'
export default defineConfig({
vite: {
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
},
})

That’s all you need to set up your import alias!