Detailed explanation of the loading rules of the require method in node.js

Detailed explanation of the loading rules of the require method in node.js

Loading rules of require method

  1. Prioritize loading from cache
  2. Core Modules
  3. Modules in the form of paths
  4. Third-party modules

1. Prioritize loading from cache

main.js: execute and load a.js module

require('./a')

a.js: executes and loads the b.js module, and outputs that a is loaded

require('./b')
console.log('a.js is loaded')

b.js: Output b is loaded

console.log('b.js is loaded')

result:

insert image description here

It can be seen that: main loads a.js, and then a does not print twice that a.js is loaded when loading b.js. Node will directly take out the exports value of the object from require.cache according to the passed id, and will not execute the module code again.

2. Core Modules

The essence of the core module is also a file. The core module file has been compiled into the binary file. We only need to load it according to the name. like:

  • require('fs')
  • require('http')

3. Path-based modules

The path-based module we are talking about is actually loading the JS file you wrote. There are four ways to load it.

var fooExports = require('./index') //Relative path, commonly used var fooExports = require('../index') //Relative path, commonly used var fooExports = require('/index') //Root directory, not commonly used var fooExports = require('D:/demo/index') //Root directory, not commonly used

4. Third-party modules

  1. All third-party modules must be downloaded through npm
  2. When using it, you can load it by requiring ('package name')
  3. It is impossible for any third-party package to have the same name as a core module.

It is neither a core module nor a path-based module, or a third-party module. The loading method is as follows

Take var template = require('art-template') as an example:

  • First find the node_modules directory in the directory where the current file is located
  • Then find the main attribute in the file as follows
  • The main attribute records the entry module of art-template
  • Then load and use this third-party package, but in fact the final load is still the file
  • If the package.json file does not exist or the entry module specified by main does not exist
  • Then node will automatically find index.js in the directory, which means index.js will be used as a default option.
  • If none of the above conditions are met, the node_modules directory in the parent directory will be searched.
  • If the previous level does not have it, continue to search up the previous level.
  • If the current disk root directory is still not found, an error message will be given: can not find module xxx

Third-party module search order: node_modules/art-template > package.json file > main property > index.js

This is the end of this article about the detailed explanation of the loading rules of the require method in node.js. For more relevant content about the loading rules of the node.js require method, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Introduction to require.resolve method in Node.js
  • Simple simulation of the require loading mechanism in node.js
  • Node.js require() source code interpretation
  • Detailed explanation of require in node.js
  • Node.js uses require() function to load modules
  • A brief analysis of the working principle of require in Node.js

<<:  Detailed explanation of the basic commands of Firewalld firewall in Centos7

>>:  Practical method of deleting a row in a MySql table

Recommend

Using zabbix to monitor the ogg process (Linux platform)

The ogg process of a database produced some time ...

CSS container background 10 color gradient Demo (linear-gradient())

grammar background: linear-gradient(direction,col...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

How to solve the problem of case insensitivity in MySQL queries

question Recently, when I was completing a practi...

Using JS to implement a rotating Christmas tree in HTML

<!DOCTYPE HEML PUBLIC> <html> <hea...

Overview and differences between html inline elements and html block-level elements

Block-level element features : •Always occupies a ...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Learn the common methods and techniques in JS arrays and become a master

Table of contents splice() Method join() Method r...

Sharing ideas on processing tens of millions of data in a single MySQL table

Table of contents Project Background Improvement ...

Analysis of the methods of visual structure layout design for children's websites

1. Warm and gentle Related address: http://www.web...

Notes on using $refs in Vue instances

During the development process, we often use the ...

19 MySQL optimization methods in database management

After MySQL database optimization, not only can t...