最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

babeljs - JavaScript - ES6 modules support using babel - Stack Overflow

programmeradmin1浏览0评论

I am working on a JS application without any JS framework. I am using babel for arrow functions support etc. I wanted to use ES6 modules in my application. If I transpile JS code which contains import and export statements and run in browser, I was getting error saying "exports is not defined".

I tried different solutions available online. But nothing worked.

Can't I use ES6 modules just by adding babel and transpiling? If yes, how can I achieve that?

In utils.js

export default Utils = {

};

In app.js

import Utils from './utils';

In gulpfile.js

.pipe(babel({
        presets: ['es2015']
}))

I am working on a JS application without any JS framework. I am using babel for arrow functions support etc. I wanted to use ES6 modules in my application. If I transpile JS code which contains import and export statements and run in browser, I was getting error saying "exports is not defined".

I tried different solutions available online. But nothing worked.

Can't I use ES6 modules just by adding babel and transpiling? If yes, how can I achieve that?

In utils.js

export default Utils = {

};

In app.js

import Utils from './utils';

In gulpfile.js

.pipe(babel({
        presets: ['es2015']
}))
Share Improve this question edited Jun 28, 2019 at 15:35 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Mar 25, 2019 at 16:55 KalyanKalyan 3043 silver badges10 bronze badges 3
  • show the code that you are using. – Tarun Dugar Commented Mar 25, 2019 at 17:00
  • @TarunDugar Updated post with code – Kalyan Commented Mar 25, 2019 at 17:06
  • are you running the build code in the browser or the source code? There has to be a build step before you can ship ES6 code to the browser. – Tarun Dugar Commented Mar 25, 2019 at 17:22
Add a ment  | 

1 Answer 1

Reset to default 4

While Babel can convert ES6 module syntax to CommonJS module syntax, it won't bundle modules for use in environments which don't support them at all.

For that, you should use Browserify, Webpack or another module bundler.

For more information you should see:

  • ES6 development environment made easy with babel, gulp and webpack!
  • Transpiling ES6 Modules to AMD & CommonJS Using Babel & Gulp (which also covers using Browserify)
发布评论

评论列表(0)

  1. 暂无评论