我在一个 Node.js 模块中找到了以下合同:
module.exports = exports = nano = function database_module(cfg) {...}
我想知道module.exports
和exports
之间的区别,以及为什么两者都在这里被使用。
我在一个 Node.js 模块中找到了以下合同:
module.exports = exports = nano = function database_module(cfg) {...}
我想知道module.exports
和exports
之间的区别,以及为什么两者都在这里被使用。
var module = new Module(...);
var exports = module.exports;
无论你做什么,只要记住当你从其他地方引用模块时,module.exports
而不是exports
将会被返回。所以当你做如下操作时:exports.a = function() {
console.log("a");
}
exports.b = function() {
console.log("b");
}
module.exports
指向的对象添加两个函数a
和b
,因此返回结果的typeof
将是一个object
:{ a: [Function], b: [Function] }
。module.exports
而不是exports
,你将得到相同的结果。module.exports
像一个导出值的容器一样运作的情况。然而,如果你只想导出一个构造函数,那么你应该了解一些关于使用module.exports
或exports
的事情。请记住,当你需要某个东西时,将返回module.exports
,而不是exports
。module.exports = function Something() {
console.log('bla bla');
}
typeof
返回的结果是'function'
,你可以使用require
来引入它并立即调用它,例如:var x = require('./file1.js')();
,因为你将返回的结果覆盖为一个函数。exports
时,你不能像这样使用:exports = function Something() {
console.log('bla bla');
}
var x = require('./file1.js')(); //Error: require is not a function
exports
时,引用不再指向
指向module.exports
的对象,所以exports
和module.exports
之间不再有关联。在这种情况下,module.exports
仍然指向空对象{}
,将被返回。exports
的意义是什么?如果只是变量重新赋值,为什么不总是使用 module.exports
?这对我来说似乎很困惑。 - user677526exports.something
比写module.exports.something
更简便。 - Srleexports
添加属性,您实际上确保返回了一个“典型”的模块导出对象。相比之下,通过使用module.exports
,您可以返回任何想要的值(原始值、数组、函数),而不仅仅是对象(这是大多数人期望的格式)。因此,module.exports
提供了更多的功能,但也可以用于导出非典型值(如原始值)。相比之下,exports
更为限制,但更安全(只要简单地向其添加属性而不重新分配它)。 - Marcus Junius Brutus设置module.exports
允许在required
时像调用函数一样调用database_module
函数。仅设置exports
将不允许导出函数,因为Node.js导出的是module.exports
所引用的对象。以下代码将不允许用户调用该函数。
以下代码是无效的。
exports = nano = function database_module(cfg) {return;}
如果设置了module.exports
,下面的代码将会起作用。
module.exports = exports = nano = function database_module(cfg) {return;}
控制台
var func = require('./module.js');
// the following line will **work** with module.exports
func();
基本上,node.js不会导出当前exports
所引用的对象,而是导出了原始引用exports
的属性。虽然Node.js会导出module.exports
所引用的对象,使您可以像调用函数一样调用它。
他们同时设置了module.exports
和exports
,以确保exports
没有引用先前导出的对象。通过同时设置两者,您可以使用exports
作为简写,并避免日后可能出现的潜在错误。
使用exports.prop = true
而不是module.exports.prop = true
可以节省字符并避免混淆。
nano.version = '3.3'
而不是module.exports.version ='3.3'
之类的语句,这样读起来更清晰一些。(请注意,nano
是一个局部变量,在设置模块导出之前稍微声明了一下。) - josh3736module.exports
但没有设置 exports
,我的代码还能正常工作吗?感谢任何帮助! - Asad Saeeduddinmodule.exports
,这个函数就可以正确导出。 - Lime基本上,答案在于当一个模块被通过 require
语句引入时实际发生了什么。假设这是第一次需要该模块。
例如:
var x = require('file1.js');
file1.js的内容:
module.exports = '123';
当执行上述语句时,会创建一个Module
对象。它的构造函数是:
当执行上述语句时,将创建一个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 = [];
}
正如你所看到的,每个模块对象都有一个名为 exports
的属性。这最终作为 require
的一部分返回。
require
的下一步是将 file1.js 的内容封装到一个匿名函数中,如下所示:
(function (exports, require, module, __filename, __dirname) {
//contents from file1.js
module.exports = '123;
});
下面是对这个匿名函数的调用方式,这里的module
指的是之前创建的Module
对象。
(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,请确保按照以下方式进行。
exports = module.exports = {};
如果我们按照错误的方法进行操作,module.exports
仍将指向作为模块实例的一部分创建的对象。
exports = {};
因此,向上述导出对象添加任何内容都不会对module.exports对象产生影响,也不会作为require的一部分被导出或返回。
exports = module.exports = {};
(意为:输出等于模块的输出,都为空对象)。 - Giant Elkfunc()
在@William的回答中失败了! - turtledove最初,module.exports=exports
,而require
函数返回的是module.exports
所引用的对象。
如果我们向该对象添加属性,比如说exports.a=1
,那么module.exports和exports依然指向同一个对象。因此,如果我们调用require并将模块分配给一个变量,那么这个变量就有一个名为a的属性,其值为1;
但是,如果我们覆盖它们中的一个,例如exports=function(){}
,那么它们现在是不同的:exports指向一个新对象,而module.exports指向原始对象。如果我们要求文件,它将不会返回新对象,因为module.exports没有引用到新对象。
对于我来说,我会继续添加新属性,或将它们都覆盖为一个新对象。只覆盖其中一个是不正确的。并且请记住,module.exports
才是真正的老板。
exports
和 module.exports
是相同的,除非您在模块内部重新分配 exports
。
最简单的思考方式是将此行隐式放置在每个模块的顶部。
var exports = module.exports = {};
如果在你的模块中重新赋值了exports
,那么你就在模块内部对其进行了重新赋值,它将不再等于module.exports
。这就是为什么如果你想要导出一个函数,你必须这样做:
module.exports = function() { ... }
如果你只是将你的function() { ... }
分配给exports
,那么你会重新分配exports
,使其不再指向module.exports
。
如果你不想每次都用module.exports
来引用你的函数,可以这样做:
module.exports = exports = function() { ... }
请注意,module.exports
是最左边的参数。
附加属性到exports
上不同于重新分配它。这就是为什么这个能够工作。
exports.foo = function() { ... }
这与JavaScript中通过引用传递对象的方式有微妙的不同。
exports
和module.exports
都指向同一个对象。 exports
是一个变量,而module.exports
是模块对象的属性。
假设我写了以下代码:
exports = {a:1};
module.exports = {b:12};
exports
和 module.exports
现在指向不同的对象。修改 exports
不再修改 module.exports
。
当 import 函数检查 module.exports
时,它会得到 {b:12}
const b = require()
让我得到 {b:12}
,我如何导入以获得 {a:1}
? - GoodMan我只是做了一些测试,结果发现,在Node.js的模块代码中,应该像这样:
var module.exports = {};
var exports = module.exports;
exports = function(){}; // this will not work! as it make the exports to some other pointer
module.exports = function(){}; // it works! cause finally nodejs make the module.exports to export.
exports.abc = function(){}; // works!
exports.efg = function(){}; // works!
module.exports = function(){}; // from now on we have to using module.exports to attach more stuff to exports.
module.exports.a = 'value a'; // works
exports.b = 'value b'; // the b will nerver be seen cause of the first line of code we have do it before (or later)
module.exports
是Node使用的“真正的东西”,但在某个时候,您需要将所有的exports
添加到module.exports
中,除非您正在使用exports.namespace
(上面的第2种情况),在这种情况下,似乎就像Node运行了一个extends(module.exports, exports);
将exports
的所有“命名空间”添加到module.exports
对象中?换句话说,如果您正在使用exports
,那么您可能希望在其上设置属性? - Cody要了解区别,必须先了解Node.js在运行时对每个模块所做的操作。Node.js为每个模块创建一个包装函数:
(function(exports, require, module, __filename, __dirname) {
})()
请注意第一个参数exports
是一个空对象,第三个参数module
是一个具有多个属性的对象,其中一个属性名为exports
。这就是exports
的来源和module.exports
的来源。前者是一个变量对象,后者是module
对象的属性。
在模块内部,Node.js会自动执行以下操作:module.exports = exports
,最终返回module.exports
。
因此,如果你重新分配某个值给exports
,它对module.exports
没有任何影响。(仅仅因为exports
指向另一个新对象,而module.exports
仍然保持旧的exports
)
let exports = {};
const module = {};
module.exports = exports;
exports = { a: 1 }
console.log(module.exports) // {}
但是如果您更新exports
的属性,它肯定会影响到module.exports
。因为它们都指向同一个对象。
let exports = {};
const module = {};
module.exports = exports;
exports.a = 1;
module.exports.b = 2;
console.log(module.exports) // { a: 1, b: 2 }
还要注意,如果您将另一个值重新分配给module.exports
,那么对于exports
的更新似乎就毫无意义了。 因为module.exports
指向另一个对象,所以对exports
的每个更新都将被忽略。
let exports = {};
const module = {};
module.exports = exports;
exports.a = 1;
module.exports = {
hello: () => console.log('hello')
}
console.log(module.exports) // { hello: () => console.log('hello')}
以下是摘自Manning出版社的《node.js in action》中关于Node.js模块的良好描述。
最终在应用程序中导出的内容是module.exports。而exports只不过是对module.exports的全局引用,最初被定义为空对象,可以向其中添加属性。因此,exports.myFunc仅仅是module.exports.myFunc的简写形式。
因此,如果将exports设置为其他任何值,都会破坏module.exports和exports之间的引用关系。因为真正被导出的是module.exports,所以exports将无法按预期工作-它不再引用module.exports。如果要保持这个链接,可以按以下步骤让 module.exports再次引用 exports:
module.exports = exports = db;
app.js
:
var ...
, routes = require('./routes')
...;
...
console.log('@routes', routes);
...
exports = function fn(){}; // outputs "@routes {}"
exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"
module.exports = function fn(){}; // outputs "@routes function fn(){}"
module.exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"
./routes/index.js
:
module.exports = require('./not-index.js');
module.exports = require('./user.js');
exports = function fn(){};
exports = function user(){};
./routes/index.js
:
module.exports.fn = require('./not-index.js');
module.exports.user = require('./user.js');
exports = function fn(){};
exports = function user(){};
./routes/index.js
:
module.exports.fn = require('./not-index.js');
module.exports.user = require('./user.js');
exports.fn = function fn(){};
exports.user = function user(){};
user.js
更改为{ ThisLoadedLast: [Function: ThisLoadedLast] }
,我们得到输出结果"@routes { ThisLoadedLast: [Function: ThisLoadedLast] }"。./routes/index.js
...module.exports.fn = require('./not-index.js');
module.exports.ThisLoadedLast = require('./user.js');
exports.fn = function fn(){};
exports.ThisLoadedLast = function ThisLoadedLast(){};
module.exports
。
exports
和module.exports
指向同一个对象,除非您重新分配其中一个。最终会返回module.exports
。因此,如果您将exports
重新分配为函数,则不应期望返回该函数,因为它不会被返回。但是,如果您像这样赋值函数exports.func = function...
,则结果将具有func属性和函数作为值。因为您将属性添加到exports
所指向的对象中。 - Muhammad Umermodule.exports
和exports
。为什么不只提供module.exports
以避免这种混淆呢? - Freewind