Home > Article > Web Front-end > Detailed explanation of Node module module
When developing complex web applications, it is usually necessary to split and encapsulate each function into different files and reference the files when needed, that is, to perform modular management of the code. Almost all programming languages have their own module organization methods, such as packages in Java and assemblies in C#, and Node adopts the CommonJS module specification.
Module Specification
CommonJS aims to standardize JS that runs outside of the browser and has solved a large number of JS issues (such as global naming conflicts). In Node's implementation of CommonJS, each module will be encapsulated in a separate JS file, that is, a file is a module, and the file path is the module name. When writing each module, the following three predefined variables are available:
require()
This function is used to To load and use other modules in the current module, pass in a module name and return a module export object. The module name can use a relative path (starting with ./) or an absolute path (starting with / or a drive letter such as C:). In addition, the .js extension in the module name can be omitted. At this time, Node will look for a folder with the same name. If it cannot be found, it will look for a js file with the same name. You can also use this function to load and use a JSON file, but the .json extension cannot be omitted.
exports
This object is the export object of the current module. It is used to export the public methods and properties of the module. It defaults to an empty object {}. When other modules use the current module through the require() function, they get the exports object of the current module. A public method is exported in the following code:
exports.hello = function() { console.log("Hello World!"); };
module
This object is used to provide the elements of the current module Data and other related information, but the most useful one is to use its exports attribute to replace the export object of the current module. For example, the module export object is a normal object by default. You can use the following method to turn it into a function:
module.exports = function() { console.log("Hello World!"); };
Note: When using the above method, all exports objects Modifications will be ignored!
Module initialization
The JS code in a module is only executed once when the module is used for the first time, and is initialized during execution. The module's exported object. Later, the cached export object is reused.
Define a module in test.js
//定义私有变量 var name = ""; function setName(n) { name = n; } function logName() { console.log(name); } //导出公有方法 exports.setName = setName; exports.logName = logName;
Load the test module in main
//加载test模块 var test1 = require("./test.js"), test2 = require("./test.js"); //使用test1 test1.setName("Neo"); //使用test2 test2.logName(); //Neo
It can be seen that no matter how many times require() is called, the same module is only loaded once, and the same instance is obtained by referencing multiple times.
Main module
The module passed to Node through the command line parameters to start the program is called the main module. The main module is responsible for scheduling and composing the entire application. Other modules of the program work together. For example, when starting the program through the following command line, main.js is the main module:
$ node main.js
Binary module
In addition to using JS to write modules, Node also supports using C/C++ to write binary modules. The compiled binary modules can be used in the same way as JS modules except that the file extension is .node. Although binary modules can use all functions provided by the operating system, they are difficult to use across platforms.
Related recommendations:
Detailed explanation of module syntax in JavaScript ES6
The above is the detailed content of Detailed explanation of Node module module. For more information, please follow other related articles on the PHP Chinese website!