Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does nuxt give me this error with vue-chartjs?

I get this error while trying to make vue-chartjs work

 WARN  in ./node_modules/vue-chartjs/es/BaseCharts.js

"export 'default' (imported as 'Chart') was not found in 'chart.js'

And also this in devtools:

TypeError: chart_js__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor

Steps to recreate:

  1. Create nuxt app with: $ yarn create nuxt-app <project_name>
  2. Add vue-chartjs with $ yarn add vue-chartjs chart.js
  3. Add following code which should work according to vue-chartjs docs

~/components/BarChart.js

import { Bar } from 'vue-chartjs'

export default {
  extends: Bar,
  props: ['data', 'options'],
  mounted() {
    this.renderChart(this.data, this.options)
  },
}

~/pages/index.vue

<template>
  <div class="container">
    <div>
      <bar-chart
        :data="barChartData"
        :options="barChartOptions"
        :height="200"
      />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      barChartData: {
        labels: [
          '2019-06',
          '2019-07',
          '2019-08',
          '2019-09',
          '2019-10',
          '2019-11',
          '2019-12',
          '2020-01',
          '2020-02',
          '2020-03',
          '2020-04',
          '2020-05',
        ],
        datasets: [
          {
            label: 'Visits',
            data: [10, 15, 20, 30, 40, 50, 60, 70, 34, 45, 11, 78, 45],
            backgroundColor: '#003f5c',
          },
          {
            label: 'Pages Views',
            data: [30, 24, 57, 23, 68, 72, 25, 64, 133, 143, 165, 33, 56],
            backgroundColor: '#2f4b7c',
          },
          {
            label: 'Users',
            data: [45, 65, 30, 53, 34, 35, 26, 37, 34, 45, 67, 87, 98],
            backgroundColor: '#665191',
          },
        ],
      },
      barChartOptions: {
        responsive: true,
        legend: {
          display: false,
        },
        title: {
          display: true,
          text: 'Google analytics data',
          fontSize: 24,
          fontColor: '#6b7280',
        },
        tooltips: {
          backgroundColor: '#17BF62',
        },
        scales: {
          xAxes: [
            {
              gridLines: {
                display: false,
              },
            },
          ],
          yAxes: [
            {
              ticks: {
                beginAtZero: true,
              },
              gridLines: {
                display: false,
              },
            },
          ],
        },
      },
    }
  },
}
</script>

<style>
/* Sample `apply` at-rules with Tailwind CSS
.container {
@apply min-h-screen flex justify-center items-center text-center mx-auto;
}
*/
.container {
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.title {
  font-family: 'Quicksand', 'Source Sans Pro', -apple-system, BlinkMacSystemFont,
    'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  display: block;
  font-weight: 300;
  font-size: 100px;
  color: #35495e;
  letter-spacing: 1px;
}

.subtitle {
  font-weight: 300;
  font-size: 42px;
  color: #526488;
  word-spacing: 5px;
  padding-bottom: 15px;
}

.links {
  padding-top: 15px;
}
</style>

I am also using tailwindcss in the project and other nuxt project options were Axios, Git, ESlint, Prettier.

like image 575
Nodedesign Avatar asked Apr 04 '21 11:04

Nodedesign


People also ask

What is Vue Chartjs?

vue-chartjs is a wrapper for Chart. js in vue. You can easily create reuseable chart components. Supports Chart.

Does Nuxt include Vue?

js is a frontend framework built upon Vue.

Is Nuxt the same as Vue?

As a good start, Vue provides assets and components directory. But when your application grows you need to organize your codes with classifications. Nuxt sets you up with configurable separate directory for application views, layout templates, and Vuex store files.

Which Vue does Nuxt use?

The current versions of nuxt. js are still using vue 2.6. x, and to make it work with vue 3 main feature (composition api), you should use the module called composition-api.


Video Answer


4 Answers

Chart.js has a new release version with 3.0.x. I think, vue-chartjs does not support it yet.

You can downgrade chart.js and try again.

ChartJS [3.0.1 - Published 2 days ago] https://www.npmjs.com/package/chart.js?activeTab=readme

And there is a vue-chartjs issue about 22 hours ago. https://github.com/apertureless/vue-chartjs/issues/695

like image 149
umutcakir Avatar answered Oct 21 '22 02:10

umutcakir


chart.js version above 3.0.0 is not compatible, you need to downgrade chart.js npm install [email protected]

This has solved my issue

like image 35
Kunal Tyagi Avatar answered Oct 21 '22 00:10

Kunal Tyagi


maybe its a bit late but I have to give the answer,

what you need is to change the version of both libraries

npm install [email protected] [email protected] --save

it works fine in vue2

like image 40
PHM La Avatar answered Oct 21 '22 01:10

PHM La


yarn add vue-chartjs [email protected]

if you are using yarn

like image 30
MarioBe Avatar answered Oct 21 '22 00:10

MarioBe