Import Aliases in Vite

If you're migrating from Vue CLI to Vite, you may find that the @ alias no longer works. In this guide, we'll show you how to configure vite alias, vite resolve alias, and vite import alias to clean up your imports. Learn how to set up aliases in your vite.config.js file and improve your development workflow by using simpler paths instead of long relative imports like ../../../someComponent.vue.
Understanding Vite Aliases

Vite provides an easy way to configure import aliases through the resolve.alias option in its configuration file. This feature allows you to create shorthand paths for directories, making your imports cleaner and more manageable.
How to Set Up Vite Aliases
To configure a custom alias in Vite, you need to adjust your vite.config.js file. Below is a simple example of how to set up the @ alias for the src directory, a common setup for Vue projects:
// vite.config.js
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// Vite resolve alias configuration
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)) // Alias for src folder
}
}
})
Once the alias is configured, simply restart your Vite development server, and you will be able to use @ in your imports instead of long relative paths.
Common Use Cases for Vite Aliases
Now that you know how to set up the alias, let's explore some common use cases where aliases can simplify your development workflow:
-
Importing components: With the alias set, you can import components like this
import MyComponent from '@/components/MyComponent.vue'
Instead of using long relative paths.
Bonus Tips for Migrating from Vue CLI to Vite
Here are a few extra tips to help smooth the transition from Vue CLI to Vite:
requireno longer works for importing images in Vite. To learn more about handling static assets in Vite, check out the official Vite documentation.- When importing single-file components, remember to include the
.vueextension, as Vite no longer automatically resolves it.
Related Resources
If you want to dive deeper into Vite configurations and learn more about managing imports, you can check out the following resources:
- Rapid Development with Vite Course on Vue School
- Vite Resolve Alias Documentation
- @rollup/plugin-alias
Conclusion
Setting up custom import aliases in Vite is a quick and easy way to clean up your code and improve the overall development experience. By using the resolve.alias configuration, you can avoid long, messy relative paths and make your project more maintainable. Whether you're working with Vue or another framework, Vite's alias system is a powerful tool to simplify your imports.
Related Courses
Start learning Vue.js for free

Comments
Latest Vue School Articles
5 Component Design Patterns to Boost Your Vue.js Applications

Vibe Coding a Collaborative Editor with Comment Support with Nuxt UI and Jazz

Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.


