Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 command 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.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>

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

like image 773
Joël Hecht Avatar asked Oct 29 '25 21:10

Joël Hecht


1 Answers

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"
  }
}
like image 101
Igor Kiselov Avatar answered Oct 31 '25 11:10

Igor Kiselov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!