Laravel, Vue - "You may need an appropriate loader" for template tags

11.6k Views Asked by At

I am currently building a web application using Laravel and Vue. The error that I am getting is for all the components that I have.

ERROR in ./resources/assets/js/components/App 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <template>
|     <div id="layout-wrapper">
|         <app-topbar></app-topbar>
 @ ./resources/assets/js/app.js 6:0-35 41:11-14
 @ multi ./resources/assets/js/app.js ./resources/assets/sass/style.scss

the App.vue file looks like this:

<template>
    <div id="layout-wrapper">
        <app-topbar></app-topbar>
        <app-menu></app-menu>

        <div class="main-content">
            <div class="page-content">
                <router-view></router-view>
                <app-footer></app-footer>
            </div>
        </div>
    </div>
</template>

<script>
    import AppTopbar from './_partials/AppTopbar'
    import AppMenu from './_partials/AppMenu'
    import AppFooter from './_partials/AppFooter'
    import Users from './users/Users'
    import Dashboard from './dashboard/Dashboard'

    export default {
        components: {
            'app-topbar': AppTopbar,
            'app-menu': AppMenu,
            'app-footer': AppFooter,
            Users,
            Dashboard,
        },
        beforeMount() {
            axios.get('/metadata')
                .then(response => {
                    window.Laravel = {
                        response: response,
                        current_year: response.current_year,
                        csrf: response.csrf,
                        asset(path){
                            return this.response.asset+'/'+path;
                        },
                        trans(text){
                            return this.response.trans[text];
                        },
                        route(name){
                            return this.response.base_url+'/'+this.response.routes[name];
                        }
                    };
                });
        },
    }
</script>

From the error, I understand that the problem is that <template></template> tags are not being parsed. From what I found out I need the vue-loader and vue-template-compiler dependencies for them. I have already installed those, however, I still keep getting these errors. I also found that trying the older version (current 15.x.x , older 14.x.x) of vue-loader could help, but it did not.

My versions are: laravel 8.12.3 npm 6.14.4 package.json dependencies:

"devDependencies": {
        "axios": "^0.19",
        "cross-env": "^7.0",
        "laravel-mix": "^5.0.1",
        "lodash": "^4.17.19",
        "resolve-url-loader": "^3.1.0",
        "sass": "^1.29.0",
        "sass-loader": "^8.0.2",
        "vue-template-compiler": "^2.6.12"
    },
    "dependencies": {
        "vue": "^2.6.12",
        "vue-loader": "^14.2.4",
        "vue-router": "^3.4.9"
    }

Since I am not skilled in Vue, I understand that I did something wrong. However, I have not found solutions online. So, I would like to ask for your help.


Update: Since Laravel has a pre-configured webpack.config file then probably, this is what I think I am able to show you. webpack.mix.js :

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel applications. By default, we are compiling the CSS
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.sass('resources/assets/sass/style.scss', 'public/assets')
    .js('resources/assets/js/app.js', 'public/js')

    .styles([
        // Layout
        'resources/assets/template/system/css/bootstrap.min.css',
        'resources/assets/template/system/css/icons.min.css',
        'resources/assets/template/system/css/app.min.css',
        // Data Table
        'resources/assets/template/system/css/dataTables.bootstrap4.min.css',
        'resources/assets/template/system/css/responsive.bootstrap4.min.css',
        // Custom
        'public/assets/style.css'
    ], 'public/css/style.min.css')

    .scripts([
        // Layout
        'resources/assets/template/system/js/jquery.min.js',
        'resources/assets/template/system/js/bootstrap.bundle.min.js',
        'resources/assets/template/system/js/metisMenu.min.js',
        'resources/assets/template/system/js/simplebar.min.js',
        'resources/assets/template/system/js/waves.min.js',
        'resources/assets/template/system/js/jquery.sparkline.min.js',
        'resources/assets/template/system/js/morris.min.js',
        'resources/assets/template/system/js/raphael.min.js',
        'resources/assets/template/system/js/app.js',
        // Data Tables
        'resources/assets/template/system/js/jquery.dataTables.min.js',
        'resources/assets/template/system/js/dataTables.bootstrap4.min.js',
        'resources/assets/template/system/js/dataTables.responsive.min.js',
        'resources/assets/template/system/js/responsive.bootstrap4.min.js',
        // Vue
        //'resources/assets/template/system/js/vue.js',
        // Custom
        //'resources/assets/js/vue.js',
        'public/js/app.js',
        'resources/assets/js/script.js'
    ], 'public/js/script.min.js');

4

There are 4 best solutions below

0
Manu R S On BEST ANSWER

Application Laravel 8 + Vue 2.6 versions then please change the code in the

webpack.mix.js

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        //
    ]);

To

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        //
    ]).vue();

"you may need an appropriate loader to handle this file type" laravel and vue application error reason >> package.json - "laravel-mix": "^6.0.6" is version 6 or higher.

1
Maurício Testa On

In Laravel 8 the compilation of assets works in a slightly different way. It will only add vue compilers as a dependency if you declare that you want to compile a vue file in webpack.mix.js.

See what happened to me after doing this: http://prntscr.com/whr7wv

Try changing
.js('resources/assets/js/app.js', 'public/js')
to
.js('resources/assets/js/app.js', 'public/js').vue()
and return to post if successful.

Note: consider not using laravel/ui and manually creating app.js

0
IfrahPervaiz On

Actually you have not installed ui for vue files in laravel, that's why it does not recognize the template tag in vue file. So run few commands in new terminal:

  1. composer require laravel/ui
  2. php artisan ui vue
  3. npm install && npm run dev
1
Josiah Mahachi On

This is what worked for me (Laravel 8 + Vue 2.6). Change the code in the

webpack.mix.js

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css')
    .sourceMaps();

to

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css')
    .vue();

then run

npm update vue-loader