In nodejs, modularization refers to splitting a large file into multiple small modules that are independent and dependent on each other. Each js file is considered a separate module; the modules are invisible to each other. If a module needs To use another module, you need to use the specified syntax to introduce the module, and only the content exposed by the module can be imported. The syntax is "const variable name = require('module path');".
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
concept
Background: In team collaboration, different members will independently write the code they are responsible for. For example, A is responsible for a.js, and B is responsible for b.js. If a.js and b.js define the same variable, then use the two file, an error will be reported if the variable has the same name. That is, global variable pollution. Solution->nodejs modularization
Concept: Modularization in nodejs means that each js file will be considered a separate module. Modules are invisible to each other. If a module needs to use another module, it needs to import the module to be used by specifying the syntax, and only the content exposed by the imported module can be used.
Modularization means following fixed rules and splitting a large file into multiple small modules that are independent and interdependent. In layman's terms, one js file can use data in another js file.
Syntax: including introduction and exposure
Import module
const variable name = require('module path');Exposed syntax (choose 1 from the following 2 syntaxes)
Inside the module:
1. Exposed module.exports. Attribute name 1 = data 1; module.exports. Attribute name 2 = data 2; 2. Exposed module.exports = { Attribute name 1: data 1, Attribute name: 2 data 2,}Module path details
./: current directory
../: The upper-level directory of the current directory
./next level directory/
You can omit the .js suffix when introducing modules
When introducing the nodejs module itself or the package downloaded by npm, you do not need to write the path, just write the module name directly.
1. Introduce the a.js module let in the upper-level directory. Variable name = require('./../a'); 2. Introduce the b.js module let in the lower-level directory routes. Variable name = require(' ./routes/b');3. Introduce the jQuery module let downloaded from npm. Variable name = require('jquery');Expand your knowledge:
What are the benefits of modularity?
1. Can improve code reusability
2. Can improve the maintainability of code
3. On-demand loading can be achieved (this will be very obvious when learning vue)
The emergence of modularization has also brought module scope, which is similar to function scope. Variables, methods, etc. defined in custom modules can only be accessed within the current scope. This access restriction effectively solves variable pollution. problem.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of what modularity is in nodejs. For more information, please pay attention to other related articles on this site!