简体   繁体   中英

Export default variable with BabelJS

I'm using Babel for ES2015 module definitions.

I have a file a.js :

require('babel-core/register')({presets: ['es2015']})
require('./b')

Which requires a file b.js

export default b = () => true

Babel compiles this to

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = b = function Main() {
  return true;
};

But then throws an error:

ReferenceError: b is not defined
  at Object.<anonymous> (b.js:1:16)

I believe the issue comes from babel adding strict mode as you are then referencing an undeclared variable. Changing it to:

const b = () => true
export default b;

fixed it for me.

如果这是整个模块,并且没有全局b ,则这是运行时错误,因此这可能是第一个问题:

export default b = () => true

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