Node. JS's module system

Source: Internet
Author: User

Compared to native JavaScript, it is difficult to share variables between different JavaScript files. In view of this, node. js expands on JavaScript, introducing the Require,exports,module three global object.

I.. absolute module and Relative module

Smashing node. JS's author divides the modules in node. js into two categories, absolute modules, and relative modules.

<1> absolute modules refers to the important modules of node core, such as HTTP,FS, when we use these modules, we only need require (' module_name ') Also includes third-party module installed with NPM, where the module is installed by default in the./node_modules/path, and when you use these modules, you only need to require (' module_name '). However, the name of these module is added to the Package.json file in order to install it using NPM.

<2> relative modules, refers to our own written modules, these modules generally exist within the project folder, reference we need to require (' relative path/module_name ') in the way of reference. Relative paths refer to where these modules are stored relative to the project folder.

With these modules, we can split the complex node. JS JavaScript code into different files, simplifying the management and maintenance of our project.

Second, what is require?

At the very beginning, we can simply interpret require as loading, including. For ordinary JS files, we are in a a.js file, require another b.js file, b.js file will be automatically inserted into the A.js file place.

1 name_a = ' a '; 2 Console.log (name_a); 3 // Console.log (Name_b);//error, Name_b undefined
1 name_b = ' B '2 console.log (name_b);
1 require ('./module_a ');//relative module, you need to give the path of module 2 require ('./module_b '); 3 4 Console.log (name_a); 5 Console.log (name_b);

Third, module and exports

<1> returning an instance of an object via exports:

By default, each module returns an empty object instance through exports, and we can change the object instance returned by the module by adding a property to the module's exports.

 1  module_a.js  2  3  exports.name = John;  4  exports.data = '  5  var  privatevariable = 5; 6  exports.getprivate = function   () { 7  return   privatevariable;  8 }; 
1 Index.js 2 3 var a = require ('./module_a ');  Note that exports returns an object instance that has already been created by default, so you can call the property directly without the need for new. 4 Console.log (a.name); 5 Console.log (a.data); 6 Console.log (A.getprivate ());

<2> return constructor function via Module.exports:

While it is possible to pass a created object instance through Module.exports, sometimes we want to control the creation of the object when it is passed, and we need to pass the constructor function:

 1  module.exports = person;  2   function   person (name) { 4  this . Name = name;  5   6  7  Person.prototype.talk = function   () { 8  console.log (' My name is ', Span style= "color: #0000ff;" >this  .name);  9 } 
1 var person = require ('./person '); 2 var New Person (' John '); // The person is the constructor function and must be new before it can be used 3 john.talk ();

As for the use of module.exports and exports there are some differences, module.exports for export type (only with module.exports), exports is used to export an instance of the object (although Module.exports You can also refer to the following blog post.

Http://www.cnblogs.com/pigtail/archive/2013/01/14/2859555.html

Node. JS's module system

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.