[英]Chart v4 : Module not found: Error: Package path . is not exported from package node_modules/chart.js
I'm maintaining a react component library that uses chart.js as a dependency (Not peerDependency).我正在维护一个使用 chart.js 作为依赖项(不是 peerDependency)的反应组件库。
I upgraded chart.js from 3.9.1 to 4.0.1.我将 chart.js 从 3.9.1 升级到 4.0.1。
My library still compiles fine but now it throws an error on my react app when I'm importing the lib:我的库仍然可以正常编译,但现在当我导入库时,它会在我的 React 应用程序上引发错误:
Module not found: Error: Package path. is not exported from package node_modules/chart.js
I'm building everything with rollup here is my config:我正在使用 rollup 构建所有内容,这是我的配置:
import path from 'path';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import postcss from 'rollup-plugin-postcss';
import typescript from '@rollup/plugin-typescript';
import image from '@rollup/plugin-image';
import json from '@rollup/plugin-json';
import copy from 'rollup-plugin-copy';
import autoprefixer from 'autoprefixer';
import postcssUrl from 'postcss-url';
import pkg from './package.json' assert { type: 'json' };
import {fileURLToPath} from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const { dependencies = {}, peerDependencies = {} } = pkg;
const externals = [...Object.keys(dependencies), ...Object.keys(peerDependencies)];
const src = path.resolve(__dirname, 'src');
const input = path.resolve(src, 'index.ts');
const assets = path.resolve(src, 'assets');
const dest = path.resolve(__dirname, 'dist');
export default [
{
input,
external: (id) => externals.some((dep) => id === dep || id.startsWith(`${dep}/`)),
plugins: [
typescript({ tsconfig: './tsconfig.json' }),
commonjs(),
json(),
resolve({ browser: true }),
babel({
extensions: ['.ts', '.js', '.tsx', '.jsx'],
}),
image(),
postcss({
plugins: [
autoprefixer,
postcssUrl({
url: 'inline',
basePath: assets,
}),
],
}),
copy({
targets: [
{ src: 'src/**/_*.scss.d.ts', dest },
{ src: 'src/**/_*.scss', dest },
],
}),
],
output: [
{
file: pkg.main,
format: 'cjs',
},
{ name: pkg.name, file: pkg.module, format: 'es' },
{
name: pkg.name,
file: pkg.browser,
format: 'umd',
globals: {
react: 'React',
},
},
{
name: pkg.name,
file: pkg['browser:min'],
format: 'umd',
globals: {
react: 'React',
},
plugins: [terser()],
},
],
},
];
Does someone has any idea why it is doing so?有人知道为什么要这样做吗?
I tried to delete node_modules and package-lock.json and reinstall both on my library and front app but I still have the same error.我试图删除 node_modules 和 package-lock.json 并在我的库和前端应用程序上重新安装,但我仍然有同样的错误。
I tried to import from 'chart.js/auto' as mentioned in the documentation but it throws:我尝试按照文档中所述从“chart.js/auto”导入,但它抛出:
Module not found: Error: Package path./auto is not exported from package node_modules/chart.js
I looked at node_modules/chart.js/package.json file and there is a well defined set of exports there.我查看了 node_modules/chart.js/package.json 文件,那里有一组定义明确的导出。 But as it came with the upgrade and no other library has ever throw me this error I guess it comes from the upgrade.
但是由于它伴随着升级而来,而且没有其他图书馆向我抛出这个错误,我猜它来自升级。
Found the bug.发现了错误。 As I thought it is a problem with the library itself.
我认为这是图书馆本身的问题。 It cannot be required since the conditional subpath is missing from the package.json file.
它不是必需的,因为 package.json 文件中缺少条件子路径。 Nothing I can do.
我无能为力。 I'll downgrade untill the push a fix.
我会降级,直到推动修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.