简体   繁体   中英

Error when migrating from Babel 5 to 6 (ReferenceError: exports is not defined)

I'm using Gulp and Babel to compile client-side es6 code to es5. After upgrade I got this error (in the browser): Uncaught ReferenceError: exports is not defined

The reason for this error, is that Babel compiles my client scripts as CommonJS modules and adds this lines in the beginning of each file:

Object.defineProperty(exports, "__esModule", { // <-- ReferenceError: exports is not defined
  value: true
});

But I'm not using any UMD/CommonJS module loaders on the client, so this code causes errors. With Babel 5, to avoid this, I was using option modules: 'ignore' in my gulpfile:

return gulp.src(src, {base: 'src'})
   .pipe(babel({
        modules: 'ignore' // <-- dropped from Babel 6
   }))
   .pipe(gulp.dest(dest));

so it compiled my scripts as is, raw and clear. But this option was dropped from Babel 6 and now it causes error [ReferenceError: [BABEL] ..myscript.js: Unknown option: base.modules] , so I had to comment this line.

Is there any alternative to modules: 'ignore' in Babel 6?

Since you are using es2015 this set of plugins enabled by default. Note that babel-plugin-transform-es2015-modules-commonjs is in there.

If you wish to not perform any type of module conversion, you'll need to explicitly list the plugins that you'd like to use, rather than using es2015 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM