You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rollup打包React库时遭遇ERR_REQUIRE_ESM错误求助

关于ERR_REQUIRE_ESM错误的排查困境

我已经花费数日尝试解决ERR_REQUIRE_ESM错误,目前正基于另一个用Rollup.js打包的React库创建新React库,以下是我的相关配置:

Rollup配置

export default [
  {
    input: "src/index.ts",
    output: [
      {
        file: packageJson.main,
        format: "cjs",
        sourcemap: true,
        inlineDynamicImports: true,
      },
      {
        file: packageJson.module,
        format: "esm",
        sourcemap: true,
        inlineDynamicImports: true,
      },
    ],
    plugins: [
      peerDepsExternal(),
      json(),
      resolve({
        preferBuiltins: true,
        mainFields: ["browser", "module", "main"],
      }),
      commonjs(),

      typescript({ tsconfig: "./tsconfig.json" }),
      // terser(),
    ],
  },
  {
    input: "dist/esm/types/index.d.ts",
    output: [{ file: "dist/index.d.ts", format: "esm" }],
    plugins: [dts()],
  },
];

package.json相关内容

...
...
"main": "dist/cjs/index.js",
  "module": "dist/esm/index.js",
  "types": "dist/index.d.ts",
  "peerDependencies": {
    "ethers": ">=5.5.1",
    "react": "^18.2.0",
    "wagmi": "0.9.x"
  },
  "files": [
    "dist"
  ],
  "exports": {
    ".": "./dist/index.js"
  },
...
...

更新:我也尝试了DTS插件,但问题依旧存在。


内容的提问来源于stack exchange,提问作者Rodrigo Borba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:55:08