Others, such as the Third-party modules (Third-party modules) or local modules (which are installed through NPM), expose a public API for each module. So that developers can import. Such as
Copy Code code as follows:
var mod = require (' module_name ')
After this sentence is executed, the inside node is loaded with built-in modules or modules installed through NPM. The Require function returns an object that exposes an API that may be functions, objects, or properties such as functions, arrays, or even any type of JS object.
The loading and caching mechanism of the node module is listed here
1 loaded into the built-in module (A Core module)
2 Loading file module (A)
3 Loading file directory module (A Folder module)
4) loading into the Node_modules module
5 auto-cache loaded module
One, load the built-in module
The built-in module of node is compiled into binary form, using the name instead of the file path when referencing. When a third party module and a built-in module have the same name, the built-in module overwrites the third party with the same name module. Therefore, you need to be careful not to have the same name as a built-in module. such as getting an HTTP module
Copy Code code as follows:
var http = require (' http ')
The HTTP that is returned is the built-in module that implements the HTTP function node.
Second, loading file module
of an absolute path
Copy Code code as follows:
var mymod = require ('/home/base/my_mod ')
or relative path of the
Copy Code code as follows:
var mymod = require ('./my_mod ')
Note that the extension ". js" is ignored here, and the following is the equivalent
Copy Code code as follows:
var mymod = require ('./my_mod ')
var mymod = require ('./my_mod.js ')
Third, loading file directory module
You can directly require a directory, assuming that you have a directory named folder, such as
Copy Code code as follows:
var mymod = require ('./folder ')
At this point, node searches the entire folder directory, and node assumes that the folder is a package and tries to find the package definition file Package.json. If the folder directory contains no Package.json files, node assumes that the default primary file is Index.js, which loads index.js. If Index.js does not exist, then the load will fail.
If the directory structure is as follows
Package.json is defined as follows
Copy Code code as follows:
{
' Name ': ' Pack ',
"Main": "Moda.js"
}
At this point require ('./folder ') returns the module moda.js. If Package.json does not exist, then the module index.js is returned. If Index.js does not exist, a load exception will occur.
Iv. loading modules into the Node_modules
If the module name is not a path or is not a built-in module, node will attempt to search the Node_modules folder in the current directory. If the current directory is not found in the Node_modules, node will search from the node_modules of the parent directory so that it recursively goes down to the root directory.
Don't worry, the NPM command allows us to easily install, uninstall, and update the Node_modules directory.
Automatic caching of Loaded modules
The loaded module node is cached without the need to search again each time. Here is an example
Moda.js
Copy Code code as follows:
Console.log (' module Moda start loading ... ')
Exports = function () {
Console.log (' Hi ')
}
Console.log (' module Moda loading complete ')
Init.js
Copy Code code as follows:
var mod1 = require ('./moda ')
var mod2 = require ('./moda ')
Console.log (mod1 = = = MOD2)
Command line execution:
Node Init.js
Enter the following
You can see that although the require is two times, the moda.js is still only executed once. Mod1 and Mod2 are the same, that is, two references all point to the same module object.