Rollup 3.x 版本 打包报错: Original error: Cannot use import statement outside a module

发布于2023年2月07日 16:00:48 ,最后一次修改于2023年6月22日 11:43:55

平台:window 11

Node: v14.18.3

rollup: ^3.14.0

打包时出现下面错误:

`(node:6152) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use 'node --trace-warnings ...' to show where the warning was created)
[!] 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
https://rollupjs.org/command-line-interface/#bundleconfigascjs
F:\cloudtrek\@cloudtrek\wxapp-radar-sdk\rollup.config.js:3
import babel from '@rollup/plugin-babel'`

解决方案:

尝试在github上的解决方案,亲测无效。https://github.com/rollup/rollup/issues/4446

two way fix this:
change rollup.config.js to rollup.config.mjs
add "type":"module" to package.json

完美解决方案:

修改package.json文件:

在build命令后添加 --bundleConfigAsCjs 即可

"scripts": {
    "build": "npm run clean && rollup -c rollup.config.js --bundleConfigAsCjs",
    "clean": "rimraf ./dist",
}