Commonjs with Notes

Source: Internet
Author: User

Modularity of the Commonjs

The node application consists of modules, each with its own scope, and functions, variables, classes, and so on are private.

COMMONJS specifies that within each module, the module variable (which is an object) represents the current module and module.exports is an external interface. Other files require are loaded by means of this interface to obtain the data that the module outputs.

Features of the COMMONJS module:

    • All code runs at the module scope and does not pollute the global scope;
    • The module can be loaded multiple times, but will only run once at the first load, then cache the results and load the results directly from the cache at a later time;
    • The order in which modules are loaded is in the order in which they appear in the code
Module Object

Node inside provides a Module build function, all modules are Module instances; inside each module, there is an Module object representing the current module with the following properties:

    • ID--Usually a module file name with an absolute path
    • FileName--Module file name
    • Loaded--Boolean, whether the load has completed
    • Parent--Module object that invokes the module
    • Children--An array of other modules referenced to
    • Exports--The value of the module's external output
Require command

Basic function: Read in and execute a JavaScript file, return the module's exports object, if not found the specified module will be error;

requireThe load file suffix name defaults to .js ;

Path Loading rules:

    • "/"--the module file of the absolute path;
    • "./" begins with a relative path to the directory where the module currently executing the script is located;
    • Not the path starting with "/" or "./", find the location of the first identity, and then find the following path as a parameter;
    • If the specified module file is not found, node will attempt to add to the file name, and .js .json .node then re-search, and resolve by the corresponding file type;

Directory load: require When a parameter string points to a directory, the file for that directory is automatically viewed package.json , and then the main portal file specified by the field is loaded, or if there is no main field or package.json file, the directory index.js or file is loaded index.node .

requirecommands to load duplicate modules (identified by absolute path) are actually executed only once, followed by loading the cache; All caches are stored in require.cache . Deleting a module's cache can write the following code:

//删除指定模块delete require.cache[moduleName];//删除所有模块Object.keys(require.cache).forEach(function(key) {    delete require.cache[key];})
Loading mechanism

The COMMONJS load module is a copy of the loaded output value, which is not affected by the internal changes in the source module after loading.

Reference: COMMONJS Specification

Commonjs with Notes

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.