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:
Improve code reusability
Improve the maintainability of the code
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.
Classification of modules in Node.js
Node.js divides modules into three categories according to their different sources:
Built-in modules (built-in modules are officially provided by Node.js, such as fs, path, http, etc.)
Custom module (every .js file created by the user is a custom module. A relative path needs to be added when loading)
Third-party modules (unofficial, need to be downloaded before use)
Load Module
The three modules above can be loaded using the
require()
method. Using therequire()
method will execute the code in the loaded module.1
2
3const 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 advanceNote that the
.js
suffix can be omitted when loading custom modules using therequire()
method.
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
2const 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 errorsayHello()
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).
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:
Inside each module, the module variable represents the current module.
The module variable is an object whose exports property (ie
module.exports
) is the external interface.Loading a module is actually loading the module.exports attribute of the module. The
require()
method is used to load modules.