Home >Web Front-end >JS Tutorial >How to import module in nodejs? Introduction to the execution process of require

How to import module in nodejs? Introduction to the execution process of require

青灯夜游
青灯夜游forward
2021-06-18 10:42:223449browse

This article will give you a detailed introduction to the execution process of require in nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to import module in nodejs? Introduction to the execution process of require

I was looking at node recently, so I deliberately learned about the process of introducing its modules. Today I will make a review record, I hope it will be helpful to you who are reading

Module classification in node

  • Core module, or built-in Module (that is, the fs, http, vm that comes with node...)For details, please see the official website api

  • Third-party modules, or use your own encapsulated npm package for private use Library or public library (installed and used through npm install on npm)

  • File modules, internal files of the project, require using relative paths or absolute paths

[Recommended learning: "nodejs Tutorial"]

Module import require

My debugging tools The

How to import module in nodejs? Introduction to the execution process of require

require process that comes with vscode is used

#1. Read the file and enter the require method return mod.require (path)

How to import module in nodejs? Introduction to the execution process of require

#2. Enter mod.require and find that Module.prototype.require is actually executed (the id here is the initial path), um~ Why? Module._load is called, it’s nothing, let’s continue

How to import module in nodejs? Introduction to the execution process of require

3. Enter Module._load(),Module._load(id, this, /* isMain */ false) Load the module according to the path

  • Use the file name as the key of the Module._cache object to query whether the file has been cached. If the cache is hit, export directly

How to import module in nodejs? Introduction to the execution process of require

  • If it does not exist in the cache array, Module._resolveFilename resolves the file path to an absolute path, and then persistently checks the cache. If the cache is hit, exports

How to import module in nodejs? Introduction to the execution process of require

  • The first two cache searches exclude external modules, and then determine whether they are built-in modules. If so, export them

How to import module in nodejs? Introduction to the execution process of require

    ##Going here really means that this module has really not been cached, then create a new

How to import module in nodejs? Introduction to the execution process of require

4. Enter new Module () and create an empty Object this.exports = {}: This is the exports object we ultimately want, (modules referenced in other modules will not be studied in depth here)

How to import module in nodejs? Introduction to the execution process of require

5, Module._cache [filename] The path parsed in step 3 is used as the key, and the object created in step 4 is used as the value and stored in our cache array. keep going! !

How to import module in nodejs? Introduction to the execution process of require

6. module.load(), enter the function

How to import module in nodejs? Introduction to the execution process of require

How to import module in nodejs? Introduction to the execution process of require

  • findLongestRegisteredExtension calculates the file name suffix,

  • Module._extensions[extension](this, filename) calls different loading methods according to different suffixes (here is Strategy mode)

  • What I hit here is .js. In this method, fs.readFileSync is used to read the file synchronously, and then the module._compile(content, filename) is called as parameters

How to import module in nodejs? Introduction to the execution process of require

7. module._compile() enter , and found that content was used as the parameter of wrapSafe(filename, content, this) and executed

How to import module in nodejs? Introduction to the execution process of require

8. Then enter wrapSafe() ps: I will talk about the old version directly here, execute It’s easier to understand

How to import module in nodejs? Introduction to the execution process of require

  • First call Module.wrap, enter the function, and the node module is packaged into a must-have package (function (){ }), forming An independent module

How to import module in nodejs? Introduction to the execution process of require

  • and then use the returned wrapped string module as vm.runInThisContext parameter to execute the module code (same as new Function in js The effect can be to turn the string into js for execution), for details of node's own built-in methods, please refer to node official

9. At this point, the require process of a module has been executed, and the user will get it by default. The return result of module.export

Summary

  • Read the file

  • Read the file Wrap a function in the file later

  • Function format function (exports, module, require,_dirname, filename){}

  • Place the module through runThisContext Turn into js syntax for calling

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to import module in nodejs? Introduction to the execution process of require. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete