Follow me to learn node. js (4) node. js module loading method and mechanism

  • 2020-03-30 03:11:32
  • OfStack

Others, such as third-party or local modules installed through the NPM, each expose a public API. So that developers can import. Such as


var mod = require('module_name')

After this sentence is executed, the Node internally loads the built-in module or the module installed through the NPM. The require function returns an object whose API may be functions, objects, or properties such as functions, arrays, or even any type of JS object.

The loading and caching mechanisms for the node module are listed here

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

First, load the built-in module

Node's built-in modules are compiled in binary form and referenced directly by name rather than by file path. When a third-party module has the same name as a built-in module, the built-in module overrides the third-party module with the same name. So be careful not to have the same name as the built-in module. For example, get an HTTP module


var http = require('http')

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

Load file module

Absolute path

 
var myMod = require('/home/base/my_mod')


Or relative paths

 
var myMod = require('./my_mod')


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

 
var myMod = require('./my_mod')
var myMod = require('./my_mod.js')


Three, load file directory module

You can simply require a directory, assuming you have a directory named folder, for example

 
var myMod = require('./folder')

At this point, Node will search the entire folder directory, and 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 assumes that the default main file is index.js, which loads index.js. If index.js also does not exist, then the load will fail.

Suppose the directory structure is as follows

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201406/201464101700893.png? 201454101710 ">

Package.json is defined as follows


{
    "name": "pack",
    "main": "modA.js"
}

At this point, require('./folder') returns the module moda.js. If package.json does not exist, the module index.js is returned. If index.js does not exist, then a load exception occurs.

Load the modules in node_modules

If the module name is not a path or a built-in module, Node will attempt to search the node_modules folder in the current directory. If node_modules in the current directory is not found, Node searches the node_modules in the parent directory, recursively down to the root directory.

Don't worry, the NPM command makes it easy to install, uninstall, and update the node_modules directory.

Five, automatic cache has been loaded module

The loaded module Node is cached instead of having to be searched every time. Here is an example

What modA. Js


console.log(' The module modA Start loading ...')
exports = function() {
    console.log('Hi')
}
console.log(' The module modA To load ')

Init. Js

 
var mod1 = require('./modA')
var mod2 = require('./modA')
console.log(mod1 === mod2) 

Command line execution:

The node init. Js

Enter the following

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201406/201464101729221.png? 201454101745 ">

You can see that even though it requires twice, moda.js is still only executed once. Mod1 and mod2 are identical, meaning that both references point to the same module object.


Related articles: