Javascript 在 Babel 6.x 中不能 require() 默认导出值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33704714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't require() default export value in Babel 6.x
提问by XGHeaven
In Babel 5.x, I can write the following code:
在 Babel 5.x 中,我可以编写如下代码:
app.js
应用程序.js
export default function (){}
index.js
索引.js
require('babel/register');
require('./app')();
Then, I can run node index.js
with no errors. However, using Babel 6.x, running the following code
然后,我可以毫无错误地运行node index.js
。但是,使用 Babel 6.x,运行以下代码
index.es6.js
index.es6.js
require('babel-core/register');
require('./app')();
results in an error
导致错误
require(...) is not a function
require(...) 不是函数
I want to know why?
我想知道为什么?
回答by Igor Raush
TL;DR
TL; 博士
You have to use
你必须使用
require('./app').default();
Explanation
解释
Babel 5 used to have a compatibility hack for export default
: if a module contained only one export, and it was a default export, it was assigned to module.exports
. So, for example, your module app.js
Babel 5 曾经export default
对module.exports
. 因此,例如,您的模块app.js
export default function () {}
would be transpiled to this
将被转译为这个
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = function () {};
module.exports = exports["default"];
This was done purelyfor compatibility with require
-ing Babel-transpiled modules (like you are doing). It was also inconsistent; if a module contained both named and default exports, it could not be require
-d.
这样做纯粹是为了与require
-ing Babel-transpiled 模块兼容(就像你在做的那样)。它也不一致;如果模块同时包含命名导出和默认导出,则不能是require
-d。
In reality, according to the ES6 module spec, a default export is no differentthan a named export with the name default
. It is just syntactic sugar which can be statically resolved at compile time, so this
实际上,根据 ES6 模块规范,默认导出与名称为 的命名导出没有什么不同default
。它只是可以在编译时静态解析的语法糖,所以这
import something from './app';
is the same as this
和这个一样
import { default as something } from './app';
That being said, it appears that Babel 6 decided to drop the interoperability hack when transpiling modules. Now, your module app.jsis transpiled as
话虽如此,看来 Babel 6 决定在转译模块时放弃互操作性黑客。现在,您的模块app.js被转译为
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function () {};
As you see, no more assignment to module.exports
. To require
this module, you need to do
如您所见,不再分配给module.exports
. 对于require
这个模块,你需要做
require('./app').default();
回答by haotang
Just to follow up with the correct answer above.
只是为了跟进上面的正确答案。
If you wanna use default export behavior of babel@5
, you can try babel-plugin-add-module-exportsplugin.
如果你想使用 的默认导出行为babel@5
,你可以尝试babel-plugin-add-module-exports插件。
It's working pretty well for me.
它对我来说效果很好。
回答by Ska
If this doesn't work
如果这不起作用
require('./app').default()
use
用
require('./app').default
Without the function call at the end.
最后没有函数调用。