RollupError:Node 尝试将您的配置加载为 ES 模块,即使它可能是 CommonJS

Lau*_*ani 1 reactjs rollupjs

我正在尝试在 React 中构建一个组件库,并使用 Rollup 来捆绑东西。这是我第一次使用它,我看了几个教程并遵循了他们的设置(像这样)。这是我的rollup.config.js文件:

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";

const packageJson = require("./package.json");

export default [
    {
        input: "src/index.ts",
        output: [
            {
                file: packageJson.main, //CommonJS
                format: "cjs",
                sourcemap: true,
            },
            {
                file: packageJson.module, //ES6
                format: "esm",
                sourcemap: true, 
            }
        ],
        plugins: [
            resolve(),
            commonjs(),
            typescript({ tsconfig: "./tsconfig.json" }),
        ]
    },
    {
        input: "dist/esm/types/index.d.ts",
        output: [{ file: "dist/index.d.ts", format: "esm" }],
        plugins: [dts()],
    }
];
Run Code Online (Sandbox Code Playgroud)

现在,当我运行汇总时,出现以下错误。我尝试更改文件扩展名或按照建议使用标志,但解决方案不起作用。想法?

在此输入图像描述

小智 5

您可以尝试以下步骤:

  1. 将行添加"type": "module"到您的package.json文件中
  2. 将此行添加import packageJson from './package.json' assert { type: 'json' };到您的rollup.config.js文件中

我希望它会有用