Rollup: ReferenceError: exports is not defined

Created on 16 Jul 2018  路  3Comments  路  Source: rollup/rollup

First off I don't even know if rollup is the tool i should be using or not.
I have a few TS files which are compiled into JS. I now want to avoid having to refer to run my code in Chrome browser. I don't care if my code runs in node.js or not.
If my assumption is correct then this is how i proceeded:

  • installed rollup using npm install -g
  • did a --help and it looked like the option --format iife is what i need
  • ran rollup against the entry script in a folder where only my js files resided:
    rollup test_entry.js --file bundle.js --format iife
  • referenced the resulting bundle.js in my html:
    <script src='bundle.js'></script>
  • launched the html file in chrome with the developer extension open.
  • i got the above error against this line:
    Object.defineProperty(exports, "__esModule", { value: true });

Most helpful comment

Hi I am facing the same issue. In tsconfig target is ES2015, module is commonjs.For rollup format is set to "umd". But still In the generated JS is see Object.defineProperty(exports, "__esModule", { value: true });.

Could you provide me the right combination of target, module and format so that I don't get the exports option in generated module.

I am using gulp-rollup in the below format:
.pipe(rollup({ format: "umd", moduleName: "Bundle", entry: PATHS.bundleJSMainFile }))

All 3 comments

Your approach looks sound to me. I am a little puzzled that your code contains the line Object.defineProperty(exports, "__esModule", { value: true }) as this line should only be added for formats cjs, umd and amd.

Is this line added by rollup or is it already part of your code? Are you using rollup-plugin-typescript? Otherwise you should make sure that the TypeScript is compiled to ES6 modules, not CommonJS (compilerOptions.module = 'ES6') unless you want to add rollup-plugin-commonjs as well.

Lukas thanks very much for weighing on this
I went back and changed the target in typescript.json to ES2015 from CommonJS.
I also changed the format on rollup command line to "umd"
The result is now runnable with no issues in Chrome.
Just wanted to add that the simplicity of "rollup" usage is amazing!
Closing this

Hi I am facing the same issue. In tsconfig target is ES2015, module is commonjs.For rollup format is set to "umd". But still In the generated JS is see Object.defineProperty(exports, "__esModule", { value: true });.

Could you provide me the right combination of target, module and format so that I don't get the exports option in generated module.

I am using gulp-rollup in the below format:
.pipe(rollup({ format: "umd", moduleName: "Bundle", entry: PATHS.bundleJSMainFile }))

Was this page helpful?
0 / 5 - 0 ratings