我正在尝试学习如何使用rollup来打包一些javascript,下面是一些在网上的教程。我一开始就试图使用一个配置文件。使用命令行,一切看起来都正常,但是配置文件以Unexpected token 'export'语法错误结束。
我的配置是Windows 11,它具有:
c:\dev\rollup-test>node --version
v16.14.2
c:\dev\rollup-test>rollup --version
rollup v2.70.1配置文件:
c:\dev\rollup-test>type rollup.config.js
export default {
input: './src/main.js',
output: {
file: './build/bundle.js',
format: 'es'
}
};javascript文件src\main.js
c:\dev\rollup-test>type src\main.js
console.log('hi there!');在CLI模式下,一切似乎都正常:
c:\dev\rollup-test>rollup ./src/main.js --file ./build/bundle.js --format es
./src/main.js → ./build/bundle.js...
created ./build/bundle.js in 29ms尝试使用上面的配置文件:
c:\dev\rollup-test>rollup --config
[!] SyntaxError: Unexpected token 'export'
c:\dev.local\rollup-test\rollup.config.js:1
export default {
^^^^^^
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1032:15)
at Module._compile (node:internal/modules/cjs/loader:1067:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .js] (C:\Users\joelh\AppData\Roaming\npm\node_modules\rollup\dist\shared\loadConfigFile.js:617:13)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at loadConfigFromBundledFile (C:\Users\joelh\AppData\Roaming\npm\node_modules\rollup\dist\shared\loadConfigFile.js:622:42)
c:\dev\rollup-test>也许有这么明显的事情,但可惜我看不见。有人能帮我走得更远吗?
发布于 2022-10-23 20:11:41
我也有同样的问题。
解决方案是在package.json "type":“模块”中添加一行,
package.json
{
"name": "Webpack, rollup",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"Webpack",
"rollup"
],
"author": "Igor Kiselov",
"license": "ISC",
"dependencies": {
"rollup": "^3.2.3"
}
}
https://stackoverflow.com/questions/71698183
复制相似问题