Node.js - module.exports 与 exports 的区别

简而言之 exports是 module.exports本地文件中的快捷方式(相当于引用),它不能很好的实现导出功能。

a.js

exports.f = 123123

b.js

const a = require('./a');
console.log(a.f);

这样可以正常从a导出,在b.js中引用
a.js中改为exports = {f: 123}无法导出
a.js中改为module.exports = {f: 123}正常导出

以下为官方文档翻译:

module.exports#

Added in: v0.1.16

  • <Object>
    module.exports对象是由Module系统创建的,有时这是不能被接受的;很多人希望他们的module是一些class的实例。为此,将期望导出的对象赋值到module.exports。需要注意的是赋值期望的对象到exports会简单的将本地exports变量重新绑定,这很可能并不是所期望的结果。

The module.exportsobject is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what is desired.

举个例子,假设我们建了一个叫a.js的模块
For example suppose we were making a module called a.js

const EventEmitter = require('events');

module.exports = new EventEmitter();

// Do some work, and after some time emit
// the 'ready' event from the module itself.
// 搞点事情,过一秒之后将'ready'事件从模块自身提交出去
setTimeout(() => {
  module.exports.emit('ready');
}, 1000);

在另一个文件中我们可以这么做
Then in another file we could do

const a = require('./a');
a.on('ready', () => {
  console.log('module a is ready');
});

要注意给module.exports赋值必须立即执行,而不能在回调中进行,以下为错误示例:

Note that assignment to module.exports must be done immediately. It cannot be done in any callbacks. This does not work:

x.js:

setTimeout(() => {
  module.exports = { a: 'hello' };
}, 0);

y.js:

const x = require('./x');
console.log(x.a);

exports shortcut#

Added in: v0.1.16
exports变量在模块的文件级范围内是有效的,并且会在模块评估之前被赋值为module.exports的值

它允许有一个快捷键,所以module.exports.f = ...可以更简洁的写为exports.f = ...然而,要意识到,和其他变量一样,假如有一个新的值被赋到exports上,那么它将不再绑定到module.exports:

The exportsvariable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated.

It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = .... However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports:

module.exports.hello = true; // Exported from require of module 从需求模块中导出
exports = { hello: false };  // Not exported, only available in the module 没有导出,仅在模块中有效

module.exports属性被完全替换为一个新的对象时,通常也会重新赋值exports,例如:
When the module.exports property is being completely replaced by a new object, it is common to also reassign exports, for example:

module.exports = exports = function Constructor() {
  // ... etc.
};

为了阐明这个行为,猜想一下require()的假想实现(比实际简单很多):
To illustrate the behavior, imagine this hypothetical implementation of require(), which is quite similar to what is actually done by require():

function require(/* ... */) {
  const module = { exports: {} };
  ((module, exports) => {
    // Module code here. In this example, define a function.
    function someFunc() {}
    exports = someFunc;
    // At this point, exports is no longer a shortcut to module.exports, and
    // this module will still export an empty default object.
    module.exports = someFunc;
    // At this point, the module will now export someFunc, instead of the
    // default object.
  })(module, module.exports);
  return module.exports;
}

原文链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • 今天被携程....幼师...打孩子.....喂芥末......安眠药....这些关键词霸屏了。 微博,公众号,头条...
    菲凡说阅读 618评论 0 49
  • 总有人错过一场梅雨 迷路在四月的柳絮 隔着千里暮霭找寻 天地不懂 总有人拜了一路的庙宇 只求一场精打细算的相遇 纵...
    胡子不归阅读 495评论 0 6
  • 滴滴的雨,微微的风,开着小窗,宿舍四处凉爽。 三人已睡,呼声四起,伴随着风,参杂着雨,我却毫无睡意。 抬起手机,简...
    古德曼here阅读 215评论 0 2
  • GCC: GNU Compiler CollectionGCC属于传统编译器,传统编译器的工作原理基本上都是三段...
    IRONYT阅读 4,163评论 0 1
  • 《郭靖黄蓉》 英雄少年思家国 蓉女佳人最婀娜 桃花岛上双雕落 星云梦里羡黄郭 《七夕》 星云河汉雨细细 牵牛织女影...
    人间正道是沧桑lq阅读 211评论 0 0