Others such as third-party modules or local modules installed through NPM, each module will expose a public API. So that developers can import. like
The code copy is as follows:
var mod = require('module_name')
After this sentence is executed, the built-in module or module installed through NPM will be loaded inside Node. The require function returns an object whose exposed API may be a function, object, or attribute such as a function, array, or even arbitrary type JS object.
Here are the loading and caching mechanisms of node modules
1) Load the built-in module (A Core Module)
2) Load the file module (A File Module)
3) Load the file directory module (A Folder Module)
4) Load the module in node_modules
5) Automatically cache loaded modules
1. Load the built-in module
Node's built-in module is compiled into binary form, and the name is directly used instead of the file path when referenced. When a third-party module and a built-in module have the same name, the built-in module will override the third-party module with the same name. Therefore, when naming, you need to be careful not to have the same name as the built-in module. If you get an http module
The code copy is as follows:
var http = require('http')
The returned http is a built-in module that implements the HTTP function Node.
2. Load the file module
Absolute path
The code copy is as follows:
var myMod = require('/home/base/my_mod')
or relative path
The code copy is as follows:
var myMod = require('./my_mod')
Note that the extension ".js" is ignored here, and the following is peer
The code copy is as follows:
var myMod = require('./my_mod')
var myMod = require('./my_mod.js')
3. Load the file directory module
You can directly require a directory, assuming that there is a directory named folder, such as
The code copy is as follows:
var myMod = require('./folder')
At this point, Node will search the entire folder directory, Node will assume that the folder is a package and try to find the package definition file package.json. If the folder directory does not contain the package.json file, Node will assume that the default main file is index.js, which means that index.js will be loaded. If index.js does not exist, then the loading will fail.
If the directory structure is as follows
package.json is defined as follows
The code copy is as follows:
{
"name": "pack",
"main": "modA.js"
}
At this time require('./folder') will return the module modA.js. If package.json does not exist, the module index.js will be returned. If index.js does not exist, a load exception will occur.
4. Load the module in node_modules
If the module name is not a path or a built-in module, Node will try to search in the node_modules folder of the current directory. If the node_modules in the current directory are not found, the Node will search from the node_modules in the parent directory, and then recursively continues until the root directory.
Don't worry, the npm command allows us to install, uninstall and update the node_modules directory very conveniently.
5. Automatically cache loaded modules
The Node for the loaded module is cached without having to re-search every time. Here is an example
modA.js
The code copy is as follows:
console.log('Module modA starts loading...')
exports = function() {
console.log('Hi')
}
console.log('Module modA has been loaded')
init.js
The code copy is as follows:
var mod1 = require('./modA')
var mod2 = require('./modA')
console.log(mod1 === mod2)
Command line execution:
node init.js
Enter as follows
It can be seen that although required twice, modA.js is still executed only once. mod1 and mod2 are the same, that is, both references point to the same module object.