相关疑难解决方法(0)

CommonJs模块系统中"module.exports"和"exports"之间的区别

在此页面(http://docs.nodejitsu.com/articles/getting-started/what-is-require)上,它声明"如果要将导出对象设置为函数或新对象,则必须使用module.exports对象."

我的问题是为什么.

// right
module.exports = function () {
  console.log("hello world")
}
// wrong
exports = function () {
  console.log("hello world")
}
Run Code Online (Sandbox Code Playgroud)

我console.log结果(result=require(example.js)),第一个是[Function]第二个{}.

你能解释一下背后的原因吗?我在这里阅读帖子:在Node.js中的module.exports vs exports.它很有帮助,但没有解释它以这种方式设计的原因.如果直接退回出口参考会有问题吗?

javascript commonjs node.js

253
推荐指数
5
解决办法
10万
查看次数

Babel 6.x中不能要求()默认导出值

在Babel 5.x中,我可以编写以下代码:

app.js

export default function (){}
Run Code Online (Sandbox Code Playgroud)

index.js

require('babel/register');
require('./app')();
Run Code Online (Sandbox Code Playgroud)

然后,我可以运行node index.js没有错误.但是,使用Babel 6.x,运行以下代码

index.es6.js

require('babel-core/register');
require('./app')();
Run Code Online (Sandbox Code Playgroud)

导致错误

require(...)不是函数

我想知道为什么?

javascript ecmascript-6 babeljs

78
推荐指数
3
解决办法
3万
查看次数

标签 统计

javascript ×2

babeljs ×1

commonjs ×1

ecmascript-6 ×1

node.js ×1