chartjs-plugin-datalabels: Import error: "Module 'chartjs-plugin-datalabels/types/index' has no default export."
Trying out Chart.js, and have been told I need to use the chartjs-plugin-datalabels to be able to write the percentage text on the piew pieces.
I installed and imported the chartjs-plugin-datalabels as per the documentation into my Angular 7 project (actually Ionic 4, which uses Angular 7), but vscode reports the following error on the import…
Module '"../../../../node_modules/chartjs-plugin-datalabels/types"' has no exported member 'ChartDataLabels'.ts(2
I have the following versions
"chart.js": "^2.8.0", "chartjs-plugin-datalabels": "^0.6.0",
If I go to the node_modules\chartjs-plugin-datalabels\types\index.d.ts file, I also see a similar error here for the line
declare module 'chart.js
Invalid module name in augmentation. Module 'chart.js' resolves to an untyped module at 'd:/dev/ionic/chartjstest/node_modules/chart.js/dist/Chart.js', which cannot be augmented.ts
If I proceed, and try to run, I then get the compile error
[ng] ERROR in node_modules/chartjs-plugin-datalabels/types/index.d.ts(5,16): error TS2665: Invalid module name in augmentation. Module 'chart.js' resolves to an untyped module at 'D:/dev/ionic/chartjstest/node_modules/chart.js/dist/Chart.js', which cannot be augmented. [ng] src/app/home/pie-graph/pie-graph.component.ts(3,10): error TS2305: Module '"D:/dev/ionic/chartjstest/node_modules/chartjs-plugin-datalabels/types/index"' has no exported member 'ChartDataLabels'.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 29 (7 by maintainers)
@simonbrunel the correct way to import the library and get a reference to the output in Typescript is
import * as ChartDataLabels from 'chartjs-plugin-datalabels';(based on how the library is currently being exported)You can fix this issue with installing the types for Chart.js via
npm install @types/chart.js@simonbrunel - ok thanks for that!
I tried the
import 'chartjs-plugin-datalabels';and removed the plugin stuff…and now at least my
formatter()is now called.I now have
So I can now get my percentage labels!
I am sure I had tried the
import 'chartjs-plugin-datalabels';but I never had theformatter()called, but certainly seems to work nownpm i chartjs-plugin-datalabels
@Cimmeron you need to install
@types/chart.jsas suggested in other comments.I think that’s because the TS declaration doesn’t export the plugin, so
import ChartDataLabels from 'chartjs-plugin-datalabels';doesn’t work with TS. I will need to investigate what’s the best way to fix it but in the meantime, you canimport 'chartjs-plugin-datalabels';(the old way), meaning that you will not be able to register the plugin per chart (neither unregister it globally).@simonbrunel - yes for sure, I’ll include all my component code here. This is just a test app for my to try out chart.js, so is a little adhoc and untidy with me trying stuff out. But all has worked so far except for using this plugin
I created a new Ionic 4 blank application, and have installed the following…
dependencies:
devDependencies
And the component code…
I can share the whole project if that is of any help
Hello All,
How to build a doughnut chart only using chart.js and typescript?
I have the following versions “chart.js”: “^3.3.2” and “@types/chart.js”: “^2.9.32”.
I am able to display the chart. But I want to achieve the below requirements as well.
I am getting the dependency error when I try to install npm chartjs-plugin-datalabels.
npm ERR! node_modules/chart.js npm ERR! chart.js@“^3.3.2” from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer chart.js@“>= 2.7.0 < 3” from chartjs-plugin-datalabels@1.0.0 npm ERR! node_modules/chartjs-plugin-datalabels
Any suggestion please to resolve this issue?
Thanks!
This is fixed by 92771df1588e80df1d5a70cbd8d53557222c3205 and will be released in v0.7.0. The proper way to include this plugin in TS will be:
@pjc2007 How do you import this plugin? can you share the code of your project so it will be easier to debug?
Those last two errors listed look like problems in your usage.
The
falseerror looks like you are setting a property wrong. Double check your usage of the library there as the types aren’t matching up.For the second one, you should import the library like
import * as ChartDataLabels from 'chartjs-plugin-datalabels';. The library isn’t exporting things like you are probably used to in Typescript.