question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import of chart.js 4 results in module not found

See original GitHub issue

Expected behavior

When I import chartjs in the style:

import Chart from 'chart.js/auto';

it should import without error.

Current behavior

When I try to import like this:

import Chart from 'chart.js/auto';

I get the following in the webpack output:

ERROR in ...

Module not found: Error: Package path ./auto is not exported from package …\node_modules\chart.js (see exports field in …\node_modules\chart.js\package.json)

node v16.15.0 npm 8.5.5 webpack 5.74.0 ts-loader babel 7.19.6

I can resolve this issue by changing the chart.js package.json to not use nested conditional exports. i.e.:

"exports": {
    ".":  "./dist/chart.js",
    "./auto": "./auto/auto.js",
    "./helpers": "./helpers/helpers.js"
  },

Instead of the current:

"exports": {
    ".": {
      "import": "./dist/chart.js",
      "types": "./dist/types.d.ts"
    },
    "./auto": {
      "import": "./auto/auto.js",
      "types": "./auto/auto.d.ts"
    },
    "./helpers": {
      "import": "./helpers/helpers.js",
      "types": "./helpers/helpers.d.ts"
    }
  },

I assume this is a node version or config issue or something? As I guess there’s no way everyone is seeing this?

Reproducible sample

none

Optional extra steps/info to reproduce

No response

Possible solution

Removing nested conditional exports does resolve this for us.

Context

Stopping us from upgrading.

chart.js version

4.0.1

Browser name and version

No response

Link to your project

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

3reactions
np-np-npcommented, Nov 24, 2022

I just did what you linked, but with some extra strings:

resolve: {
  conditionNames: ['import', 'node', 'default']
}

It was ‘default’ that was blowing up @babel/runtime (at runtime on _interopRequireDefault). It seems obvious now, but it wasn’t to me at the time. My dep was brought in through react-transition-group. And the standard Stack Overflow search for that error sends you off way in the wrong direction.

I don’t think I needed ‘node’. Others may need ‘require’. It’s kind of on the user to track the dep tree, look at the package.jsons, work out what conditionals there are and add them to the array. Which kind of sucks.

Also worth drawing attention to the webpack docs note that the order of names in the array is important as it states order of preference. Could see that tripping people up.

Thinking about it some more… conditionNames use is fraught with potential problems.

0reactions
dangreencommented, Nov 24, 2022

@np-np-np could you please share how you change webpack config ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart v4 : Module not found: Error: Package path . is not ...
I looked at node_modules/chart.js/package.json file and there is a well defined set of exports there. But as it came with the upgrade and...
Read more >
Chart.js does not import as es6 module · Issue #5179 - GitHub
I am using chart.js to visualize results of agent based models. ... I could not find a module: field in package.json, nor an...
Read more >
Installation - Chart.js
If you download or clone the repository, you must build Chart. js to generate the dist files. Chart. js no longer comes with...
Read more >
Error Can't resolve 'react-chartjs-2&#38
p>I downloaded the react Pro package, ran the npm install. and when I try to run the my application for the first time....
Read more >
Getting started – amCharts 5 Documentation
JavaScript module (ES6)#. These are available via NPM or GitHub. You can use our official npm package @amcharts/amcharts5 to install amCharts 5 into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found