17

In OS X with Laravel/Homestead, I'm getting an error using Vue (v2.2.1). The component won't load and the console error is "Uncaught TypeError: Vue.component is not a function".

Full console error

    Uncaught TypeError: Vue.component is not a function
        at eval (eval at <anonymous> (app.js:321), <anonymous>:17:5)
        at Object.<anonymous> (app.js:321)
        at __webpack_require__ (app.js:20)
        at app.js:64
        at app.js:67  

What is throwing me off is that if I make a change to app.js, the webpack does not update to reflect any changes. So I need to A) fix the issue above, and B) figure out how to get the webpack to show updates in the console.

Any help would be greatly appreciated! I'm a noob. Here is my code...

app.js file

    Vue.component('video-upload', require('./components/VideoUpload.vue'));

    const app = new Vue({
        el: 'body'
    });

VideoUpload.vue

    <template>
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="panel-heading">Example</div>

                        <div class="panel-body">
                            This is an example
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </template>

    <script>
        export default {
            mounted() {
                console.log('Component mounted.')
            }
        }
    </script>

upload.blade.php

    @extends('layouts.app')

    @section('content')
        <video-upload></video-upload>
    @endsection

package.json

    {
      "private": true,
      "scripts": {
        "prod": "gulp --production",
        "dev": "gulp watch"
      },
        "devDependencies": {
        "bootstrap-sass": "^3.3.7",
        "gulp": "^3.9.1",
        "jquery": "^3.1.0",
        "laravel-elixir": "^6.0.0-9",
        "laravel-elixir-vue": "^0.1.4",
        "laravel-elixir-webpack-official": "^1.0.2",
        "lodash": "^4.14.0",
        "video.js": "^5.11.6",
        "vue": "^2.2.1",
        "vue-resource": "^0.9.3"
      }
    }
Stephen Pappas
  • 173
  • 1
  • 1
  • 5
  • nothing's wrong with your code. so, i think laravel elixir webpack official that gives you problem here https://github.com/vuejs/laravel-elixir-vue-2/issues/20 . laravel mix is a better option. – ishadif Mar 01 '17 at 01:46

8 Answers8

59

After an update from laravel-mix 5.0.9 to laravel-mix 6.0.11 on a laravel 7 project it started to see this error on vue compiled views. I change the call the Vue package:

Use import Vue from 'vue' instead of window.Vue = require("vue"); worked for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
18

import vue properly in your code using import keyword like this:

//import vue
import Vue from 'vue';

//register component
Vue.component('yourComponentName',require('./components/yourComponentName.vue').default);

//initialize vue
const app = new Vue({
    el: '#app',
});
Qasim Nadeem
  • 597
  • 4
  • 17
12

For any one who has the same issue and is not related to Laravel.

Hint: using Vue 3+

Register the component globally:

const MyComponent = {
  // ... options ...
}

// if you do not have App component
Vue.createApp({}).component("my-component", MyComponent).mount("#app");

// if you have an App component
const App = {
  // ... options ...
}
const app = Vue.createApp(App);
app.component("my-component", MyComponent);
app.mount("#app");

Please refer to Documentation for more info

Safwat Fathi
  • 758
  • 9
  • 16
4

1

Change your package.json

"laravel-elixir-vue-2": "^0.3.0"

And run

<!-- language: lang-js -->
npm install

OR

2

npm install laravel-elixir-vue-2 --save-dev

And then

Change your gulpfile.js like this

<!-- language: lang-js -->
var elixir = require('laravel-elixir')

require('laravel-elixir-vue-2');
LaraFlow
  • 352
  • 5
  • 15
  • Thank you for the response. Once updated to Vue 2, the next console error was "[Vue warn]: Do not mount Vue to or - mount to normal elements instead." I simply wrapped the component in a
    , re-ran Gulp, and everything works beautifully. Much appreciated!
    – Stephen Pappas Mar 04 '17 at 17:51
4

CHECK YOUR CDN VUE VERSION. THEY CHANGED FROM VUE 2 TO VUE 3.

This scenarion happened here, we were using Vue 2 and our CDN changed the global script to Vue 3.

Vue 3 changed the "Vue.component" to "Vue.directive" which broke our application.

Allan Zeidler
  • 317
  • 2
  • 7
4

VUE.JS CDN changed the global script to Vue 3 which is not supported [Vue.component] any more.

Use Vue Version 2.6.14, this works for me (older apps) https://v2.vuejs.org/v2/

2
window.Vue = require('vue').default;
const app = new Vue({
    el: '#app',
});


<body>
 <div id="app">.....</div>`
</body>
saurabh
  • 2,553
  • 2
  • 21
  • 28
0

just add this

/*import Vue from 'vue/dist/vue'*/ before
/*require('./bootstrap');*/
  
require('./bootstrap');

import Vue from 'vue/dist/vue'
window.Vue = require('vue');

Vue.component('mainapp', require('./component/mainapp.vue').default);


const app = new Vue({
  el:"#app",
});
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49