Modular
Modularization refers to the process of dividing the system into several modules from top to bottom layer by layer when solving a complex problem. For the whole system, a module is a unit that can be combined, disassembled and replaced.
Modularization in the programming field is to follow fixed rules and split a large file into multiple small modules that are independent and interdependent.
Advantages of modularity:
- Improved code reusability
- Improved code maintainability
- On-demand loading can be achieved
Classification of Modules in Node.js
In Node.js, modules are divided into three categories according to different sources of modules, namely:
- Built-in modules ((built-in modules are officially provided by Node.js, such as fs, path, http, etc.)
- Custom modules (every .js file created by the user is a custom module)
- Third-party modules (modules developed by third parties are not official built-in modules or user-created custom modules, which need to be downloaded before use)
Module scope in Node.js
What is module scope
Similar to function scope, variables, methods and other members defined in custom modules can only be accessed within the current module. This kind of module-level access restriction is called module scope.
Benefits of using module scope to prevent global variable pollution
Members at module scope are shared outwards
1. module object
There is a module object in each .js custom module, which stores information related to the current
2. module.exports object
In a custom module, you can use the module.export object to share the members in the module for external use. When the outside world uses the require() method to import a custom module, what you get is the object pointed to by module.exports
If a custom object wants to transfer internal variables to the outside for external use, it is necessary to mount the corresponding properties and methods for the module.exports object inside the custom module
When using the require() method to import a module, the result of the import is always based on the object module.exports
pointed to by .
3. exports object
Because the word module.exports is more complicated to write, in order to simplify the external sharing of member codes, Node provides the exports object. By default, module.exports and exports point to the same object , and the final sharing result is still the object pointed to by module.exports prevail
4. Misunderstandings in the use of module.exports and exports
The thing to remember is that when using require() modules, you will always get the object pointed to by module.exports
In this blog, I explained
the misunderstandings of module.exports and exports in more detail
load module
Using the require() method, you can load the required built-in modules, custom modules and third-party modules, eg:
//加载内置模块(fs,path,http)
const fs = require('fs')
//加载用户的自定义模块
const index = require('./index.js')
//加载第三方模块
const moment = require('moment')
When using the require() method to load other modules, the code in the documented module will be executed
modular specification
The modular specification refers to the rules that need to be followed when the code is modularized and combined.
Node.js follows the CommonJS modular specification. CommonJS specifies the characteristics of modules and how each module depends on each other.
CommonJS specifies:
- Inside each module, the module variable represents the current module.
- The module variable is an object, and its exports attribute (ie module.exports) is an external interface.
- Loading a module is actually loading the module.exports property of the module. require) method is used to load the module