rollup 报 Unexpected token 'export'
时间: 2023-06-21 11:03:54 浏览: 169
这种错误通常是因为你的代码中使用了 ES6 的模块语法,而 Rollup 默认只支持 CommonJS 模块语法。你可以通过在 Rollup 配置文件中添加 `@rollup/plugin-commonjs` 插件来解决这个问题。
具体操作如下:
1. 安装 `@rollup/plugin-commonjs`:
```
npm install --save-dev @rollup/plugin-commonjs
```
2. 在 Rollup 配置文件中引入 `@rollup/plugin-commonjs`:
```js
import commonjs from '@rollup/plugin-commonjs';
```
3. 将 `commonjs` 添加到 `plugins` 数组中:
```js
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
commonjs()
]
};
```
这样配置之后,Rollup 就可以正确地处理 ES6 模块语法了。
相关问题
Unexpected token 'export'
This error message usually occurs when you are trying to use an ES6 module syntax such as `export` in a JavaScript environment that does not support it.
To fix this error, you can either:
1. Use a bundler like Webpack or Rollup to compile your code into a format that supports `export` statements.
2. Use a different module syntax such as `module.exports` for Node.js or global variables for browser-based JavaScript.
For example, instead of using `export` in your code, you can use `module.exports` like this:
```
module.exports.myFunction = function() {
//code here
}
```
Or, if you are working with browser-based JavaScript, you can define the variable globally like this:
```
window.myFunction = function() {
//code here
}
```
[!] 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: Unexpected token 'export'
你遇到了一个 RollupError 错误,它提示你的配置文件被当作了 CommonJS 模块来加载,但实际上它很可能是一个 ES 模块。为了解决这个问题,你可以尝试以下几种方法之一:
1. 将你的配置文件的扩展名改为 ".mjs",这样 Rollup 将会将其视为 ES 模块。
2. 在你的 package.json 文件中添加 "type": "module" 字段,告诉 Rollup 这是一个 ES 模块。
3. 使用 "--bundleConfigAsCjs" 标志来指定将配置文件作为 CommonJS 模块进行打包。
请根据你的具体情况选择其中一种方法来解决问题。希望能帮到你!如果你有其他问题,可以继续问我。