What could be causing this error involving vue-chartjs in Nuxt?

I encountered this issue while attempting to utilize vue-chartjs

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

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

In addition, I noticed the following error in devtools:

TypeError: chart_js__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor

Steps for recreation:

  1. Create a new nuxt app using:
    $ yarn create nuxt-app <project_name>
  2. Install vue-chartjs with $ yarn add vue-chartjs chart.js
  3. Add the code snippet below which should work based on vue-chartjs documentation

~/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>

In my project, I'm also utilizing tailwindcss along with other nuxt project options like Axios, Git, ESlint, Prettier.

Answer №2

To ensure compatibility, you must downgrade chart.js to a version below 3.0.0.

npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b28232a393f6521380b796572657f">[email protected]</a>

The solution provided resolved the problem I was facing.

Answer №3

Perhaps it's belated, but allow me to provide the solution,

All you need to do is update the versions of the libraries

npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d0dbd2c1c79dd9c0f3819d849d82">[email protected]</a> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bfdfeeea6e8e3eaf9ffe1f8cbb8a5bfa5bb">[email protected]</a> --save

This solution works seamlessly with vue2

Answer №4

npm install vue-chartjs <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea89828b989ec48099aad8c4d3c4de">[email protected]</a>

in case you prefer npm over yarn

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Attempted to access undefined property 'renderMenu' in a promise without handling it

I am looking to generate a dynamic menu based on the JSON data provided below: [ { "teamId": "10000", "teamName": "Laughing Heroes", "superTeamId": "", "createTime": "2017-06-25T06:07:45.000Z", "createUserId": null }, { "team ...

Javascript datatables do not allow for setting a default column sort

I am encountering an issue where I need to sort the results by a specific column on page load. In this case, I want the initial results to be displayed in descending order based on "RecordDate". However, it seems that the server side is blocking any sort ...

A collection of jQuery objects that consist of various DOM elements as their properties

Seeking a more concise and potentially more streamlined approach using jQuery. I have an object called lbl which represents a div. Inside this div, there is a span tag that contains the properties firstName and lastName of the lbl object. Here's how t ...

loop through each category in a specified class

<div class="contain_questions"> <div class="question"> <div class="question_text">First question</div> <div class="question_mandatory">1</div> <div class="options"> <div ...

What methods can Cypress use to validate content containing hyperlinks?

My current task is to develop an automation test that confirms the presence/display of content containing a hyperlink embedded within text. Please refer to the screenshot I have provided for better understanding, as it illustrates the specific content encl ...

Error encountered in React JS: The property being accessed is undefined and therefore cannot be mapped

I have been working on a MERN project and I am currently at the stage where I am integrating the frontend and backend using Axios. However, I have encountered some issues. One of the most common errors I am facing in my list screens is: TypeError: Cannot r ...

the conditional operator used without assigning the result to a variable

Exploring conditional operators on html canvas, I am seeking a streamlined approach to dynamically change stroke color based on conditions. Online examples have not provided a satisfactory solution in a single line of code. Currently, without using a cond ...

Trouble with using .className for form validation

The .className is not applying the formValidation class on getWeight.length < 1 and getHeight.length < 1. I am stuck trying to figure out why this is happening after reviewing the code extensively. Any thoughts on what could be causing this issue? Y ...

What could possibly be causing a syntax error in my JavaScript code?

<script type="text/javascript> $(document).ready(function(){ $("a.grouped_elements").fancybox( 'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 'speedIn' : 600, ...

Switching CSS styles with ng-click

Is there a way to toggle a CSS style based on an ng-click event? When the user clicks the ng-click element for the first time, the style is applied. However, when they click the ng-click element for the second time, the CSS does not change as expected to ...

Activate event using jQuery in webpack and manage it on the page

I'm currently in the process of revamping a large application using React. One challenge I've encountered is refreshing a datatable on the page after a successful form submission, as the datatable isn't controlled by React or webpack. I trie ...

Logging into Facebook using Angular 2

As I work on developing a website that requires users to log in with their Facebook account, I am facing some challenges. I am utilizing Angular 2 and TypeScript for this project, but the user information retrieval is not working as expected. Let's d ...

Scrolling down within a Bootstrap modal

I'm working on a bootstrap modal and looking to implement a scroll down feature upon button click: <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal --> ...

Building a flexible table in React JS based on specific keys: a complete guide

I'm currently working on developing a dynamic table component using React JS. The component at the moment has a fixed header with the most common result keys. Some results contain additional information such as phone numbers and degrees. I'm loo ...

Clicking on Only One Card in Material UI React

I've encountered an issue with my code: const useStyles = makeStyles({ card: { maxWidth: 345, }, media: { height: 140, }, }); export default function AlbumCard(props) { const classes = useStyles(); let ...

Creating an Ajax request in Angular using ES6 and Babel JS

I have been exploring a framework called angular-webpack-seed, which includes webpack and ES2016. I am trying to make a simple AJAX call using Angular in the traditional way, but for some reason, it is not working. export default class HomeController { ...

Problem with Bootstrap multiselect: it only opens the first time, and stops working after an ajax call

I am having trouble using Bootstrap multiselect with jQuery ajax. When I run the ajax code, the multiselect button stops working properly. To see the issue in action, click here: and look at the last multiselect dropdown. Here is the ajax code snippet: ...

Guide to prompting a browser to download a file using an XHR request

When it comes to downloading files from a server using an XHR request, how can I ensure that the browser initiates the download once the response is received? Additionally, which headers should be included by the server for this process to work seamlessl ...

I am looking to integrate a post request into my Vue.js code, but I only have experience with using curl and am unsure how to implement it using Axios

I'm looking to implement a POST request in my Vue.js code, however I'm only familiar with cURL and don't know how to write it in Axios. Here is the cURL command I have: curl -X POST -F "reference_format=bibtex" -F "file=@path/to/file.pdf;ty ...

Nuxt's production mode delays resource loading

I have encountered an issue with my Nuxt app where the CSS loads fine in development mode, but when switching to production mode, some of the styles are deferred. It seems like this may be related to the Vuetify CSS, as only some classes exist initially. ...