Modularization

  1. Modular Concept

    Modularization refers to the process of dividing the system into several modules layer by layer from top to bottom when solving a complex problem. For the entire system, modules are units that can be combined, disassembled and replaced.

    In programming, modularization is complying with fixed rules, splitting a large file into multiple small modules that are independent and interdependent.


    The benefits of code modular splitting are:

    1. Improve code reusability

    2. Improve the maintainability of the code

    3. On-demand loading can be achieved


    modularity specification is the rules that need to be followed when the code is modularized and combined. For example, what syntax is used to reference the module, and what syntax is used in the module exposed members.


  2. Classification of modules in Node.js

    Node.js divides modules into three categories according to their different sources:

    1. Built-in modules (built-in modules are officially provided by Node.js, such as fs, path, http, etc.)

    2. Custom module (every .js file created by the user is a custom module. A relative path needs to be added when loading)

    3. Third-party modules (unofficial, need to be downloaded before use)


  1. Load Module

    The three modules above can be loaded using the require() method. Using the require() method will execute the code in the loaded module.

    1
    2
    3
    const fs = require('fs') //direct loading
    const custom = require('./custom.js') //different from loading other modules, custom modules need to write relative paths
    const moment = require('moment') //Unlike built-in modules, third-party modules need to be downloaded in advance

    Note that the .js suffix can be omitted when loading custom modules using the require() method.


  1. Module scope in Node.js

    Similar to the function scope, members such as variables, methods defined in a custom module can only be accessed within the current module. This module-level access restriction is called a module scope.

    1
    2
    3
    4
    5
    6
    //js1
    const username = 'Derek'
    function sayHello(){
    console.log('Hello, I am ' + username)
    }
    sayHello()
    1
    2
    const js1 = require('./js1')
    console.log(js1) //In this case, Derek can be accessed normally, because functions and variables are in the same file.

    But note here that if sayHello() is written in the second file instead of js1, then an error sayHello() will be reported as undefined.


    The benefit of module scope is to prevent global variable pollution. The introduction of .js files in native JS does not have this mechanism, that is, variables in the imported files can be directly accessed (not accessible in strict mode).


  2. Modular Specifications in Node.js

    Node.js follows the CommonJS modularity specification. CommonJS specifies the characteristics of modules and how modules depend on each other:

    1. Inside each module, the module variable represents the current module.

    2. The module variable is an object whose exports property (ie module.exports) is the external interface.

    3. Loading a module is actually loading the module.exports attribute of the module. The require() method is used to load modules.


Share