core.js:4197 ERROR TypeError: chart_js__WEBPACK_IMPORTED_MODULE_2__ is not a constructor

Viewed 14309

I am having this error with chartJS and primeNG:

ERROR TypeError: chart_js__WEBPACK_IMPORTED_MODULE_2__ is not a constructor
    at UIChart.initChart (primeng-chart.js:48)
    at UIChart.ngAfterViewInit (primeng-chart.js:29)
    at callHook (core.js:3038)
    at callHooks (core.js:3008)
    at executeInitAndCheckHooks (core.js:2960)
    at refreshView (core.js:7243)
    at refreshComponent (core.js:8326)
    at refreshChildComponents (core.js:6965)
    at refreshView (core.js:7222)
    at refreshEmbeddedViews (core.js:8280)

in my angular.json file I have added the: "scripts": ["node_modules/chart.js/dist/Chart.js"]

ChartJS version: "chart.js": "^3.0.2", PrimeNG : "primeng": "^11.3.2", Angular CLI v : "@angular/cli": "~10.0.1",

2 Answers

High probability primeng still only works with v2 of chart.js since v3 just released this month and had some breaking changes.

Downgrading chart.js to version 2.9.4 should resolve your issue

Cahnge

import Chart from 'chart.js'

into

import { Chart, registerables } from 'chart.js'; 
import 'chartjs-adapter-moment'; // or another adapter to avoid moment
Chart.register(...registerables);

from here.

Related