chartjs-plugin-gradient icon indicating copy to clipboard operation
chartjs-plugin-gradient copied to clipboard

Webpack can't compile when I import "gradient"

Open vgross opened this issue 2 years ago • 12 comments

I have an issue when I'm trying to import this library.

I'm using webpack, and it doesn't compile the JS file and throws an error at the line:

import gradient from 'chartjs-plugin-gradient';

Here is the error message:

"./node_modules/chartjs-plugin-gradient/dist/chartjs-plugin-gradient.esm.js" contains a reference to the file "chart.js/helpers". This file can not be found, please check it for typos or update it if the file got moved.

If I try something like: import helpers from 'chart.js/helpers'; I have no error, so the helpers are found... But I keep having the issue at the line where I import gradient...

Any idea?

vgross avatar Dec 13 '22 22:12 vgross

Sorry, can not figure out what could be wrong from the information given

kurkle avatar Jan 24 '23 19:01 kurkle

I encountered the same issue

sofiia-chorna avatar Mar 08 '23 19:03 sofiia-chorna

Trying to use that plugin from TypeScript and I get a similar issue.

Slion avatar Mar 22 '23 12:03 Slion

Looks like you need to specify the full path in your import: import { isNumber, color, defined } from 'chart.js/helpers/helpers.mjs';

Slion avatar Mar 22 '23 13:03 Slion

Probably has something to do with bundler and module vs commonjs. Webpack 4?

kurkle avatar Mar 22 '23 13:03 kurkle

I'm not sure I'm rather a noob in Node.js. Looks like I'm running webpack 5.75.0. It's an easy fix once you worked it out. It's also suggested in the compilation error logs.

Slion avatar Mar 22 '23 14:03 Slion

Here are the actual error logs:

ERROR in ./node_modules/chartjs-plugin-gradient/dist/chartjs-plugin-gradient.esm.js 7:0-60
Module not found: Error: Can't resolve 'chart.js/helpers' in '.\node_modules\chartjs-plugin-gradient\dist'
Did you mean 'helpers.mjs'?
BREAKING CHANGE: The request 'chart.js/helpers' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

Slion avatar Mar 23 '23 08:03 Slion

@Slion I'm assuming you are using chart.js 4.2.1, right? Would you be able to create (a very simple) reproduction of the issue?

kurkle avatar Mar 23 '23 09:03 kurkle

I'm assuming you are using chart.js 4.2.1, right?

Using chart.js v3.9.1. I could upgrade though I guess. Gradient plugin seems to work fine once you manage to load it.

Would you be able to create (a very simple) reproduction of the issue?

I'm sure I would, not sure I will find the time though.

Slion avatar Mar 23 '23 11:03 Slion

I thought I found a proper workaround but somehow that's still not working: https://stackoverflow.com/questions/70964723/webpack-5-in-ceate-react-app-cant-resolve-not-fully-specified-routes

Slion avatar Mar 23 '23 11:03 Slion

@Slion did you figure out any solution here?

Mudit18 avatar Oct 05 '23 10:10 Mudit18

I think I just had my own copy of the plugin that fixed the import path specifying the extension. It's all fairly well explained in the error logs posted above. Ultimately though we stopped using that plugin cause our gradient design specs needed some more fancy stuff. It is a really good plugin though and helped me understand gradients in chart.js.

Slion avatar Oct 07 '23 05:10 Slion