Giter VIP home page Giter VIP logo

Comments (1)

Whobeu avatar Whobeu commented on June 1, 2024

Unfortunately, chartjs-plugin-datalabels does not play nice in a dual CommonJS/ESM environment using Node.js. I was having a bear of a time trying to get labels to work properly with ESM and Node.js. I could get the labels to display but could not get them in their correct location due to failure of "instanceof" in one of the modules. That was due to the plugin loading in CommonJS mode rather than ESM mode. I did try creating a simple package.json in the "dist" directory with

{
  "type": "module"
}

which did work but broke CommonJS support. Ultimately, after some hacking in the chartjs-plugin-datalabels directory of node_modules, I came up with a workable solution though it will get wiped if I ever do an "npm update".

First I add the following to the package.json file:

  "exports":{
    "import":"./dist/chartjs-plugin-datalabels.esm.mjs",
    "require":"./dist/chartjs-plugin-datalabels.js"
  },

Then I copy chartjs-plugin-datalabel.esm.js to chartjs-plugin-datalabel.esm.mjs. Once that is done, both CommonJS and ESM routines work fine. The crazy thing is that within the package.json file there is an entry that says

"module": "dist/chartjs-plugin-datalabels.esm.js",

IMHO, this would solve the problem of CommonJS/ESM interoperability with Node.js EXCEPT, Node.js ignores the "module" property of package.json [rolls-eyes]. So the best solution at this time is to add the "exports" property and make a .mjs copy of the ESM module.

from chartjs-plugin-datalabels.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.