简体   繁体   English

有关node.js导出的问题

[英]Questions about node.js exports

I am reading some other's code, I saw a piece of code like this 我正在阅读其他人的代码,我看到了一段这样的代码

if ( typeof module === 'object' ) {
    module.exports = BBB;
}

I am wondering 1) why using if statement here 2) when using module.exports, does it mean all BBB namespace is exported 我想知道1)为什么在这里使用if语句2)在使用module.exports时,是否意味着所有BBB名称空间都已导出

BTW, BBB is a namespace defined like 顺便说一句,BBB是一个定义为

var BBB = {};

It's likely the module is intended to work in both the browser and on the server side (presumably using node.js). 该模块可能打算在浏览器和服务器端均可工作(大概使用node.js)。

Creating a sort of wrapper for your module allows it to be used in a variety of javascript loaders such as AMD or RequireJS or CommonJS (used by node) 为您的模块创建一种包装器,可以将其用于各种javascript加载器,例如AMD或RequireJS或CommonJS(由node使用)

I'd also recommend looking at umdjs/umd (Universal Module Definition). 我还建议您查看umdjs / umd (通用模块定义)。 This repo documents how you can create a wrapper for your module so that it will work in every environment you target. 此回购记录了如何为模块创建包装器,以便在目标环境中都可以使用它。


Lastly, you can sort of think of module.exports like a return value of a function. 最后,您可以将module.exports函数的返回值。 When someone imports the module, the export is what is given to them. 当有人导入模块时,导出就是给他们的。

If this is used 如果使用

// bbb.js
module.exports = BBB;

When the module is required using (for example) 当需要使用模块时(例如)

// otherfile.js
var BBB = require('./bbb');

BBB will match the exported object. BBB将匹配导出的对象。

Check out the node.js module docs for more general help. 查看node.js模块文档以获取更多常规帮助。

它是要检测您所处的环境,模块存在于节点中,而不存在于没有浏览器或类似功能的浏览器中

Most likely the author is creating a module that may not necessarily be consumed in a CommonJS enviorment, but still is offering support for it. 作者很可能正在创建一个不一定在CommonJS环境中使用的模块,但仍在为其提供支持。

The CommonJS standard defines a few free variables, require , exports , and module . CommonJS标准定义了一些自由变量, requireexportsmodule module must be an Object. module必须是一个对象。

So when the author is checking that module is of type object , they are essentialy checking for CommonJS support, they are then assigning module.exports to BBB , so that when a consumer require 's their module BBB is returned. 因此,当作者检查module是否为object类型时,必须对CommonJS支持进行必要的检查,然后将module.exports分配给BBB ,以便在使用者require时返回其模块BBB I won't go into the details of CommonJS but you can check out the standard for more info. 我不会详细介绍CommonJS,但您可以查看标准以获取更多信息。

Why would I check for CommonJS support? 为什么要检查CommonJS支持?

Because your code is intended for use in multiple enviornments/packagers. 因为您的代码旨在用于多个环境/打包程序。 For example Browserify, and Webpack use the CommonJS standard to package code for use in the browser. 例如,Browserify和Webpack使用CommonJS标准打包用于浏览器的代码。 But when creating an API for the browser, one should assume that consumers may not be using CommonJS, therefore module will not be defined and assigning a value to module.exports will throw an error. 但是在为浏览器创建API时,应该假设使用者可能没有使用CommonJS,因此将不会定义module并为module分配值module.exports将引发错误。

1) This checks if you are using this code on the server side (NodeJS). 1)这将检查您是否在服务器端(NodeJS)上使用此代码。

2) Yes, all BBB namespace is exported 2)是的,所有BBB名称空间均已导出

Here are all necessary information: http://www.sitepoint.com/understanding-module-exports-exports-node-js/ 以下是所有必要的信息: http : //www.sitepoint.com/understanding-module-exports-exports-node-js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM