chartjs / chartjs-adapter-date-fns

date-fns adapter for Chart.js
MIT License
100 stars 35 forks source link

`No "exports" main defined` error with Vitest #59

Closed cexbrayat closed 1 year ago

cexbrayat commented 1 year ago

Running vitest in a project that uses chart.js v4 and chartjs-adapter-date-fns v2.0.1 results in:

Error: No "exports" main defined in [redacted]/node_modules/chart.js/package.json
 ❯ node_modules/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.js:8:72
      6|  */
      7| (function (global, factory) {
      8| typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js'), require('date-fns')) :
       |                                                                        ^
      9| typeof define === 'function' && define.amd ? define(['chart.js', 'date-fns'], factory) :
     10| (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Chart, global.dateFns));

This is similar to this other comment

Here is a repro using stackblitz: https://stackblitz.com/edit/vitest-dev-vitest-7iyfoz?file=test/basic.test.ts

The test just imports chartjs-adapter-date-fns.

A potential fix is to add the exports field to the adapter package.json:

 "exports": {
    ".": {
      "import": "./dist/chartjs-adapter-date-fns.esm.js",
      "require": "./dist/chartjs-adapter-date-fns.js"
    }
  }