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

Solution to Linux QT Kit missing and Version empty problem

Currently encountering such a problem My situatio...

Version numbers in css and js links in HTML (refresh cache)

background Search the keyword .htaccess cache in ...

mysql backup script and keep it for 7 days

Script requirements: Back up the MySQL database e...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

Using streaming queries in MySQL to avoid data OOM

Table of contents 1. Introduction 2. JDBC impleme...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...

linux exa command (better file display experience than ls)

Install Follow the README to install The document...

How to find identical files in Linux

As the computer is used, a lot of garbage will be...

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

Implementation of k8s deployment of docker container

Environment: (docker, k8s cluster), continue with...

How to use sed command to efficiently delete specific lines of a file

Preface Normally, if we want to delete certain li...