Node. js (4) --- module loading mode and mechanism of Node. js

Source: Internet
Author: User

For other third-party modules installed through NPM (third-party modules) or local modules, each module exposes a public API. So that developers can import data. For example

Copy codeThe Code is as follows:
Var mod = require ('module _ name ')

After this statement is executed, the Node will load the built-in module or the module installed through NPM. The require function returns an object. The API exposed by this object may be a function, object, or attribute such as a function, array, or even any type of JS object.

The loading and caching mechanisms of the node module are listed here.

1) load the built-in Module (A Core Module)
2) Load File Module (A File Module)
3) load the file directory Module (A Folder Module)
4) load the modules in node_modules
5) automatically cache loaded modules

I. Loading built-in modules

Node's built-in modules are compiled in binary format. When referenced, they directly use the name rather than the file path. When the third-party module and the built-in module have the same name, the built-in module will overwrite the third-party module with the same name. Therefore, do not name the module with the same name as the built-in module. For example, obtain an http module.

Copy codeThe Code is as follows:
Var http = require ('http ')

The returned http is the built-in module that implements the HTTP function Node.

Ii. file loading Module

Absolute path

Copy codeThe Code is as follows:
Var myMod = require ('/home/base/my_mod ')


Or relative path

Copy codeThe Code is as follows:
Var myMod = require ('./my_mod ')


Note that the extension ". js" is ignored here, and the following are equivalent.

Copy codeThe Code is as follows:
Var myMod = require ('./my_mod ')
Var myMod = require ('./my_mod.js ')

  
3. Load the file directory Module

You can directly request a directory. assume there is a directory named folder, as shown in figure

Copy codeThe Code is as follows:
Var myMod = require ('./folder ')

In this case, Node searches for the entire folder directory. Node assumes that folder is a package and tries 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, and index. js will be loaded. If index. js does not exist, loading will fail.

Assume that the directory structure is as follows:

Package. json is defined as follows:

Copy codeThe Code is as follows:
{
"Name": "pack ",
"Main": "modA. js"
}

At this time, require ('./folder') will return the modA. js module. If package. json does not exist, the module index. js is returned. If index. js does not exist, a loading exception occurs.

4. Load modules in node_modules

If the module name is neither a path nor a built-in module, Node will try to search in the node_modules folder of the current directory. If no Node is found in the node_modules directory of the current directory, the Node will search from the node_modules directory of the parent directory, so that it will recursively go down until the root directory.

Don't worry. The npm Command makes it easy for us to install, uninstall, and update the node_modules directory.

5. Automatically cache loaded modules

The loaded module Node is cached, and you do not have to search for it again. The following is an example

ModA. js

Copy codeThe Code is as follows:
Console. log ('module modA starts loading ...')
Exports = function (){
Console. log ('Hi ')
}
Console. log ('module modA loaded successfully ')

Init. js

Copy codeThe Code is as follows:
Var mod1 = require ('./moda ')
Var mod2 = require ('./moda ')
Console. log (mod1 === mod2)

Command Line execution:

Node init. js

Enter the following

We can see that although require is performed twice, modA. js is executed only once. Mod1 and mod2 are the same, that is, both references point to the same module object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.