The difference between node. JS Module.exports and Exports

Source: Internet
Author: User

Require is used to load code, while exports and module.exports are used to export code, from the Touch of node. js, they are not two strangers, on the code:

Foo.js

function () {     console.log (' a '

Test.js

var x = require ('./foo '); Console.log (x.a)//1

Foo.js

function () {  console.log (' a '= {a:2

Test.js

var x = require ('./foo '); Console.log (X.A)//2

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.

Can understand this, Each node. js execution file automatically creates a module object, and the module object creates a exports property, initializes the value to an empty object, exports references the exports property of the module object, and when Module.exports is changed Time, exports will not change. When the module is exported, the actual execution is module.exports.

The difference between node. JS Module.exports and Exports

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.