如果在使用 Rollup 写 require 而 Rollup 默认使用的是 ECMAScript 模块系统。
要解决这个问题,你需要在 Rollup 的配置文件(通常是 rollup.config.js
)中使用插件来将 CommonJS 模块转化为 ECMAScript 模块。需要配置2个插件
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
plugins: [
babel({
exclude: 'node_modules/**', // 防止打包node_modules下的文件
}),
peerDepsExternal(),
resolve(), // 解析第三方模块
commonjs(), // 将CommonJS模块转换为ES6模块
postcss() //不使用less可以删除
],
这样就可以正常使用 require 来写node脚本了