peterdickins's avatar

Alias in Vue

I am building an app with a Laravel backend api and a vue.js front end.

I am trying to create an alias so that I can import components without having to use relative paths, for example:

import {useUserStore} from "@/Store/user.js";

I have added the following to vite.config.js but it doesn't seem to work, is there anything else I need to do?

export default defineConfig({
    plugins: [
        vue(),
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    resolve: {
        alias: {
            '@': '/resources/js',
        },
    },
});
0 likes
3 replies
gych's avatar
gych
Best Answer
Level 29

Try to change your alias in vite config to this

 alias: {
	'@': path.resolve(__dirname, 'resources/js'),
},

Also don't forget to import path at the top in the vite config file

import path from 'path'; 
1 like
peterdickins's avatar

@gych thank you, that is working for me in the browser now! The only issue I am having is that PHP Storm cannot resolve "@/Store/user.js"; or __dirname

Please or to participate in this conversation.