javascript - rollup --config gives : [!] SyntaxError: Unexpected token 'export' - Stack Overflow

I'm trying to learn how to use rollup to package some javascript, following some tutorial on the n

I'm trying to learn how to use rollup to package some javascript, following some tutorial on the net. I stuck at the very beginning trying to use a config file. With mand line everything seems OK, but with a config file it ends with an Unexpected token 'export' syntax error.

My config is Windows 11 with:

c:\dev\rollup-test>node --version
v16.14.2
c:\dev\rollup-test>rollup --version
rollup v2.70.1

Config file :

c:\dev\rollup-test>type rollup.config.js
export default {
    input: './src/main.js',
    output: {
      file: './build/bundle.js',
      format: 'es'
    }
  };

The javascript file src\main.js:

c:\dev\rollup-test>type src\main.js
console.log('hi there!');

In CLI mode, everything seems OK:

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

Trying to use the config file above:

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 ObjectpileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._pile (node:internal/modules/cjs/loader:1067:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require.extensions.<puted> [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>

There's probably something so obvious, but alas I can't see it. Can anyone help me to go further ?

I'm trying to learn how to use rollup to package some javascript, following some tutorial on the net. I stuck at the very beginning trying to use a config file. With mand line everything seems OK, but with a config file it ends with an Unexpected token 'export' syntax error.

My config is Windows 11 with:

c:\dev\rollup-test>node --version
v16.14.2
c:\dev\rollup-test>rollup --version
rollup v2.70.1

Config file :

c:\dev\rollup-test>type rollup.config.js
export default {
    input: './src/main.js',
    output: {
      file: './build/bundle.js',
      format: 'es'
    }
  };

The javascript file src\main.js:

c:\dev\rollup-test>type src\main.js
console.log('hi there!');

In CLI mode, everything seems OK:

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

Trying to use the config file above:

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.pileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._pile (node:internal/modules/cjs/loader:1067:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require.extensions.<puted> [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>

There's probably something so obvious, but alas I can't see it. Can anyone help me to go further ?

Share Improve this question asked Mar 31, 2022 at 19:31 Joël HechtJoël Hecht 1,8421 gold badge18 silver badges19 bronze badges 7
  • 1 export is ES6 Module syntax. Have you added the property "type":"module" to your package.json? – Timur Commented Mar 31, 2022 at 20:05
  • Should have mentioned, if you are not using ES modules, you can use module.exports instead: rollupjs/guide/en/#using-untranspiled-config-files – Timur Commented Mar 31, 2022 at 20:14
  • Rollup expects the config file to be an ES module by default, so it should work (and it does for me, with same rollup version and node v17.3.0) – Vincent Commented Mar 31, 2022 at 21:01
  • @Tim I have no package.json. Do I have to create one? I didn't see that mentionned in rollup tutorial. – Joël Hecht Commented Mar 31, 2022 at 21:16
  • @Vincent I just installed node v17.8.0, but have no luck: the same error occurs :( – Joël Hecht Commented Mar 31, 2022 at 21:21
 |  Show 2 more ments

1 Answer 1

Reset to default 5

I had the same problem.

The solution was to add a line to package.json "type": "module",

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"
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770356a4592721.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信