Differences between exports and Module.exports in the Nodejs module

Source: Internet
Author: User

With the official API of Node. js, you can see that Node. JS itself provides many core module http://nodejs.org/api/, which are compiled into binaries that can be require (' module name ') to get The core module has the highest load priority (which is reflected when the module has the same name as the core Module)

The file module is accessed through the Require ('/filename. suffix ') require ('./filename. suffix ') requrie ('.. /filename. suffix ') to access, the file suffix can be omitted; the "/" begins with an absolute path to load, beginning with "./" and ". /"starts with a relative path and begins with"./"to indicate a file in the sibling directory

Exports and module.exports; interfaces that provide external access

Talk about their differences.

1, module import will be cached, write a number of imports, will only be directed once.

Even if the path to the import is Different. Its cache refers to the actual file name, and will not be recognized as different files because of the way in which the path is passed in

1 varOutputval = 0;//Output Value2 varincrement = 1;//Incremental3 /*Setting output Values*/4 functionseoutputval (val) {5Outputval =val;6 }7 /*Set Incremental*/8 functionsetincrement (incrementval) {9increment =incrementval;Ten } one /*Output*/ a functionPrintnextcount () - { -Outputval + =increment; the Console.log (outputval); - } - functionprintoutputval () { - Console.log (outputval); + } -Exports.seoutputval =seoutputval; +Exports.setincrement =setincrement; aModule.exports.printNextCount = printnextcount;

1 /*2 a Node. js file is a module that may be JavaScript code, json, or compiled c/s extensions. 3 two important objects:4 require is to get the module from the outside5 exports is to expose the module interface6 */7 varCounter = require ('./1_modules_custom_counter '));8Console.log (' First Call module [1_modules_custom_counter] ');9Counter.seoutputval (10);//set start counting from 10TenCounter.setincrement (10);//set the increment to ten one Counter.printnextcount (); a Counter.printnextcount (); - Counter.printnextcount (); - Counter.printnextcount (); the /* - require multiple calls to the same module does not load repeatedly - */ - varCounter = require ('./1_modules_custom_counter ')); +Console.log (' Second call module [1_modules_custom_counter] '); -Counter.printnextcount ();

2, through the exports and module.exports public methods can be accessed, but there are differences

Module.exports is the real interface, exports is just one of its auxiliary tools. The final return to the call is Module.exports instead of Exports.
All the properties and methods collected by the exports are assigned to Module.exports. Of course, There is a premise that the module.exports itself does not have any attributes or methods.
If Module.exports already has some properties and methods, the information collected by exports will be ignored.

I'm going to print exports and module.exports and see what's going on .

① (although This will result in the introduction of the module error, but this step can explain the problem , later said Reason)

1 varCounter = 0;2Exports.temp =function(){3Counter + = 10;4      this. Printnextcount =function()5     {6 Console.log (counter);7     }8 }9 varIsEq = (exports = = =module.exports);Ten Console.log (exports); one Console.log (module.exports); aConsole.log (isEq);

Results:

The method that is exported by exports is passed to Module.exports. There's no Difference.

Ii

1 varCounter = 0;2Module.exports =function(){3Counter + = 10;4      this. Printnextcount =function()5     {6 Console.log (counter);7     }8 }9 varIsEq = (exports = = =module.exports);Ten Console.log (exports); one Console.log (module.exports); aConsole.log (isEq);

Results:

Directly to the module.exports, that exports is no, they are not equal.

③: Note that the import is a bit different. It's not the same as the first big One.

As the ② here

1  var  Counter = Require ('./  '); 2  var  New Counter (); 3  Counterobj.printnextcount ();

And the first big point can be directly like this:

1  var counter = require ('./1_modules_custom_counter '); 2  Console.log (' Second call module [1_modules_custom_counter] '); 3  Counter.printnextcount ();

is to new object!

Because the functions in the exported file become member methods, you want to new an object and then call the member Method.

Explain why ① is not feasible: when I call it in another file, whether it is a direct call, or a new object is called again, error!

so, to follow these two points

1. It is best not to define module.exports and exports separately.

2.NodeJs developer recommends exporting objects with module.exports, exporting multiple methods and variables with exports

3, exports and Module.exports coverage

Directly on the code, to see who left, who disappeared

1 varCounter = 0;2 Exports.printnextcount=function()3     {4counter+=2;5 Console.log (counter);6     }7 Module.exports=function(){8counter+=10;9  this. printnextcount=function(){Ten Console.log (counter) one } a  - } - varIsEq = (exports = = =module.exports); the Console.log (exports); - Console.log (module.exports); -Console.log (isEq);

Think about who is good.

Reference the Module:

① error, indicating that exports Disappeared.

1 var counter = require ('./test3 '); 2 Counter.printnextcount ();

② left.

1 var counter = require ('./test3 '); 2 var counterobj=New  counter ()3 counterobj.printnextcount ();

As a result, the value of the object function for which it was successfully exported is output, and the first two items are collected by two interfaces, and the last exports is Overwritten.

The article is a study note, if there is a mistake, look

Differences between exports and Module.exports in the Nodejs module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.