4

I'm trying to integrate a Datatable plugin (https://www.npmjs.com/package/vuejs-datatable) in my Vue application and I'm getting an error in my console.

Uncaught TypeError: Cannot read property 'use' of undefined
    at eval (vuejs-datatable.js?b015:1)
    at Object.eval (vuejs-datatable.js?b015:1)
    at eval (vuejs-datatable.js:4)
    at Object../node_modules/vuejs-datatable/dist/vuejs-datatable.js (app.js:10170)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (selector.js?type=script&index=0!./src/views/tables/data-table.vue:2)
    at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/views/tables/data-table.vue (app.js:1438)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)

My dataTable.vue file:

<template lang="html">

  <section class="data-table">
   <datatable :columns="columns" :data="rows"></datatable>
  </section>

</template>

<script lang="js">
import Vue from 'vue'
import DatatableFactory from 'vuejs-datatable'
export default {
  name: 'DatatablePage'
}
Vue.use(DatatableFactory)
</script>

And whenever i try to use 'Vue.use(PluginName)' when integrating a plugin, i get the similar error. I'm new to VueJS. Is there anything i need to do ?

4
  • What if you move Vue.use ... to above the export default? Commented Dec 14, 2018 at 3:35
  • I'll get an error saying 'Unexpected token' and the app fails to compile Commented Dec 14, 2018 at 3:41
  • What is the version of VueJs you're using, it's greater than 2.3? Commented Dec 14, 2018 at 3:46
  • getting the same error with the same plugin (vuejs-datatable), double-checked plugin is added before the main Vue instance is initialized as per @Psidom 's answer below. Commented Jan 7, 2019 at 18:13

3 Answers 3

3

You need to add plugin before your main Vue instance is initialized; See using vue plugins here, which says:

Use plugins by calling the Vue.use() global method. This has to be done before you start your app by calling new Vue().

For your case, move

import Vue from 'vue'
Vue.use(DatatableFactory)

to your main.js, so it looks like:

import Vue from 'vue'
Vue.use(DatatableFactory)

// some other code

new Vue({
...
})
Sign up to request clarification or add additional context in comments.

Comments

2

Adding the following in weback.config.js seemed to do the trick:

module.exports = {
    resolve: {
        alias: {
            ...
            'vuejs-datatable': 'vuejs-datatable/dist/vuejs-datatable.esm.js',
            ...
        }
    },
}

Based on discussion here:

https://github.com/pstephan1187/vue-datatable/issues/50

Comments

0

I faced the same error in my vue-app.

import Vue from 'vue';
import VueRouter from 'vue-router';
...some more imports here....

Vue.use(VueRouter);

...

The error was cannot read property 'use' of undefined. Which means, it had a problem with reading Vue. I checked my package.json and package-lock.json (for whether I installed in my local). Everything seemed ok. I had both Vue and Vue-router.

Deleting node_modules and re-installing worked for me.

2 Comments

How can you delete and re-install node_modules folder from project ? Can you explain please ?
I tried what you said but the problem is not solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.