rollup 打包报错

451 阅读1分钟

报错信息

[!] RollupError: Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.

Original error: Cannot use import statement outside a module

报错原因

  • 单独使用rollup打包lodash, lodash是遵从commonjs规范。所以我借助了两个插件来解决commonjs规范的库在rollup中打包。
    pnpm add @rollup/plugin-commonjs @rollup/plugin-node-resolve -D
    
  • 我在package.json文件中添加"type" :"module" 就如上报错信息。

解决方法

package.json 里添加 "type" :"commonjs" 就解决了, 我很疑惑,盼望大佬解惑。