我在Node.js模块中找到了以下契约:

module.exports = exports = nano = function database_module(cfg) {...}

我想知道module.exports和exports之间有什么区别,为什么在这里使用它们。


当前回答

为什么这两个都用在这里

我相信他们只是想清楚module.exports、exports和nano指向同一个函数——允许您使用任意一个变量来调用文件中的函数。nano为函数的功能提供了一些上下文。

导出不会被导出(只有module.exports会被导出),那么为什么还要麻烦覆盖它呢?

冗长的权衡限制了未来错误的风险,例如在文件中使用导出而不是module.exports。它还澄清了module.exports和exports实际上指向相同的值。


module.exports与导出

只要不重新分配module.exports或exports(而是向它们都引用的对象添加值),就不会有任何问题,并且可以安全地使用exports以更简洁。

当将其中一个分配给非对象时,它们现在指向不同的位置,这可能会令人困惑,除非您有意希望module.exports是特定的(例如函数)。

将导出设置为非对象没有多大意义,因为必须在末尾设置module.exports=导出,才能在其他文件中使用它。

let module = { exports: {} };
let exports = module.exports;

exports.msg = 'hi';
console.log(module.exports === exports); // true

exports = 'yo';
console.log(module.exports === exports); // false

exports = module.exports;
console.log(module.exports === exports); // true

module.exports = 'hello';
console.log(module.exports === exports); // false

module.exports = exports;
console.log(module.exports === exports); // true

为什么将module.exports分配给函数?

更简洁!比较第二个示例的长度:

helloWorld1.js: module.exports.hello = () => console.log('hello world');

app1.js:let sayHello=require('./helloWorld1');sayHellohello;//你好,世界

helloWorld2.js: module.exports = () => console.log('hello world');

app2.js:let sayHello=require('./helloWorld2');说你好;//你好,世界

其他回答

module.export和exports都指向同一个对象,然后对模块求值。

使用require语句在另一个模块中使用模块时,添加到module.exports对象的任何属性都将可用。导出是一种快捷方式,可用于相同的事情。例如:

module.exports.add = (a, b) => a+b

相当于写:

exports.add = (a, b) => a+b

因此,只要不为导出变量分配新值,就可以了。当你这样做时:

exports = (a, b) => a+b 

当您为导出分配新值时,它不再引用导出的对象,因此将保持模块的本地状态。

如果您计划为module.exports分配一个新值,而不是向可用的初始对象添加新的财产,您可能应该考虑如下所示:

module.exports = exports = (a, b) => a+b

Node.js网站对此有很好的解释。

基本上,答案在于当通过require语句需要模块时会发生什么。假设这是第一次需要模块。

例如:

var x = require('file1.js');

file1.js的内容:

module.exports = '123';

执行上述语句时,将创建Module对象。其构造函数函数为:

function Module(id, parent) {
    this.id = id;
    this.exports = {};
    this.parent = parent;
    if (parent && parent.children) {
        parent.children.push(this);
    }

    this.filename = null;
    this.loaded = false;
    this.children = [];
}

正如您所看到的,每个模块对象都有一个带有名称导出的属性。这是最终作为需求的一部分返回的内容。

require的下一步是将file1.js的内容包装成一个匿名函数,如下所示:

(function (exports, require, module, __filename, __dirname) { 
    //contents from file1.js
    module.exports = '123;
});

这个匿名函数的调用方式如下,这里的模块引用前面创建的模块对象。

(function (exports, require, module, __filename, __dirname) { 
    //contents from file1.js
    module.exports = '123;
}) (module.exports,require, module, "path_to_file1.js","directory of the file1.js");

正如我们在函数内部看到的那样,exports的形式参数指的是module.exports。本质上,这是为模块程序员提供的便利。

然而,这种便利需要谨慎使用。在任何情况下,如果尝试将新对象分配给导出,请确保这样做。

exports = module.exports = {};

如果我们按照错误的方式执行,module.exports将仍然指向作为模块实例一部分创建的对象。

exports = {};

因此,向上面的导出对象添加任何内容都不会对module.exports对象产生任何影响,也不会作为require的一部分导出或返回任何内容。

以下是

console.log("module:");
console.log(module);

console.log("exports:");
console.log(exports);

console.log("module.exports:");
console.log(module.exports);

也:

if(module.exports === exports){
    console.log("YES");
}else{
    console.log("NO");
}

//YES

注:CommonJS规范只允许使用exports变量来公开公共成员。因此,命名的导出模式是唯一真正与CommonJS规范兼容的模式。module.exports的使用是Node.js提供的扩展,以支持更广泛的模块定义模式。

我发现这个链接对回答上述问题很有用。

http://timnew.me/blog/2012/04/20/exports-vs-module-exports-in-node-js/

添加到其他帖子节点中的模块系统

var exports = module.exports 

在执行代码之前。因此,当您想要exports=foo时,您可能需要执行module.exports=exports=foo,但使用exports.foo=foo应该可以

1.导出->用作单例实用程序2.模块导出->用作逻辑对象,如服务、模型等