Home > Article > Web Front-end > Explore how the require() method works in Node.js
Almost any Node.js developer can tell you what the require() function does, but how many of us actually know how it works? We use it every day to load libraries and modules, but its behavior is a mystery to us.
Out of curiosity, I dug into node's core code to find out what was going on under the hood. But this is not a single function. I found module.js in node's module system. This file contains a surprisingly powerful and relatively unfamiliar core module that controls the loading, compilation and caching of each file. The emergence of require() is just the tip of the iceberg.
module.jsfunction Module(id, parent) { this.id = id; this.exports = {}; this.parent = parent; // ...
Module.js mainly plays two roles within Node.js. First, it provides a foundation for all Node.js modules. Each file is a new instance of a base module that persists even after the file has been run. That's why we can attach attributes to module.exports and return them when needed.
The second major task of this module is to handle the module loading mechanism of node. The independent operation "require" function we use is actually an abstract concept of module.require, which itself is just a simple encapsulation of the Module._load function. This load method handles the actual loading of each file and starts our journey there.
Module._load Module._load = function(request, parent, isMain) { // 1. Check Module._cache for the cached module. // 2. Create a new Module instance if cache is empty. // 3. Save it to the cache. // 4. Call module.load() with your the given filename. // This will call module.compile() after reading the file contents. // 5. If there was an error loading/parsing the file, // delete the bad module from the cache // 6. return module.exports };
Module._load is responsible for loading new modules and managing the cache of modules. Caching each module loaded reduces the number of redundant file reads and can significantly speed up your application. Additionally, shared module instances allow singleton features of modules to remain in the project's state.
If a module does not exist in the cache, Module._load will create a new basic module of the file. It then tells the module to read the contents of the new files before sending them to module._compile. [1]
If you notice step #6 above, you will see that module.exports has been returned to the user. This is why when you define a public interface to use, you use exports and module.exports, because Module._load will next return the required content. I'm surprised there aren't more features here, but it would be nice if there were.
module._compile Module.prototype._compile = function(content, filename) { // 1. Create the standalone require function that calls module.require. // 2. Attach other helper methods to require. // 3. Wraps the JS code in a function that provides our require, // module, etc. variables locally to the module scope. // 4. Run that function};
This is where the real magic happens. First, a special stand-alone require function is created for this module. This is a feature we all need and are familiar with. The function itself is just a package in Module.require. It also contains some little-known auxiliary methods that are convenient for us to use:
· require(): load an external module
· require.resolve (): resolve a module name to its absolute path
· require.main: main module
· require.cache: all cached modules
· require.extensions: according to its extension, for each A valid file type can be compiled using
Once require is ready, the entire loaded source code is encapsulated in a new function that can accept require, module, exports and all other exposed variables as parameters . This is a function created solely to encapsulate the module in order to prevent conflicts with the Node.js environment.
(function (exports, require, module, __filename, __dirname) { // YOUR CODE INJECTED HERE! });
The Module._compile method is executed synchronously, so the call to Module._load can only wait until the end of this code and return module.exprts to the user.
Conclusion
So, we have understood the entire code of require and have a preliminary understanding of how it works.
If you've followed all this, then you're ready for the final secret: require('module'). That's right, the module system itself can be loaded through the module system. Inception. This may sound strange, but it allows user space to interact with the module loading system without having to delve into Node.js core. Popular modules are built like this. [2]
If you want to know more, please check the module.js source code yourself. There are enough things to give you a headache for a while. The first one can tell me what NODE_MODULE_CONTEXTS is and why it is added so people can get bonus points :)
[1] The module._compile method is only used to run JavaScript files. JSON files need to be passed JSON. parse() parses and returns
[2] However, both modules are built in private module methods like Module._resolveLookupPaths and Module._findPath You can think of this as not much better...
几乎所有的Node.js开发人员可以告诉你require()函数做什么,但我们又有多少人真正知道它是如何工作的?我们每天都使用它来加载库和模块,但它的行为,对于我们来说反而是一个谜。
出于好奇,我钻研了node的核心代码来找出在引擎下发生了什么事。但这并不是一个单一的功能,我在node的模块系统的找到了module.js。该文件包含一个令人惊讶的强大的且相对陌生的核心模块,控制每个文件的加载,编译和缓存。require() 它的横空出世,只是冰山的一角。
module.jsfunction Module(id, parent) { this.id = id; this.exports = {}; this.parent = parent; // ...
在module.js在Node.js内部主要承担两个角色。首先,它为所有的Node.js模块提供了一个基础。每个文件是基本模块new出的一个新实例,即使在该文件已经运行之后,仍然存在。这就是为什么我们能够性为module.exports附加属并在需要时返回它们。
该模块的第二大任务是处理node的模块加载机制。我们使用的独立操作的“require”函数实际上是一个抽象概念的module.require,这本身就是只是一个简单的关于Module._load功能的封装。此load方法处理每个文件的实际加载,并在那里开始我们的旅程。
Module._load Module._load = function(request, parent, isMain) { // 1. Check Module._cache for the cached module. // 2. Create a new Module instance if cache is empty. // 3. Save it to the cache. // 4. Call module.load() with your the given filename. // This will call module.compile() after reading the file contents. // 5. If there was an error loading/parsing the file, // delete the bad module from the cache // 6. return module.exports };
Module._load负责加载新的模块和管理模块的缓存。缓存加载的每个模块减少冗余文件的读取次数,并可以显著地加快您应用程序的速度。此外,共享模块实例允许单例特性的模块,保持在项目中的状态。
如果某个模块没有在缓存中存在,Module._load将创建该文件的一个新的基本模块。然后,它会告诉模块在将它们发送到module._compile之前阅读新文件的内容。[1]
如果您注意到上面的步骤#6,你会看到module.exports已被返回给用户。这就是为什么当你在定义公共接口使用时,你使用exports和module.exports,因为Module._load将接下来返回require的内容。我很惊讶,这里没有更多的功能,但如果有的话那更好。
module._compile Module.prototype._compile = function(content, filename) { // 1. Create the standalone require function that calls module.require. // 2. Attach other helper methods to require. // 3. Wraps the JS code in a function that provides our require, // module, etc. variables locally to the module scope. // 4. Run that function};
这是真正的奇迹发生的地方。首先,一个特殊的独立操作的require函数是为该模块创建的。这是我们需要的并且都熟悉的功能。而函数本身只是一个在Module.require的封装,它也包含了一些便于我们使用的鲜为人知的辅助方法:
· require():加载一个外部模块
· require.resolve():解析一个模块名到它的绝对路径
· require.main:主模块
· require.cache:所有缓存好的模块
· require.extensions:根据其扩展名,对于每个有效的文件类型可使用的编制方法
一旦require准备好了,整个加载的源代码就会被封装在一个新的函数里,可以接受require,module,exports和所有其他暴露的变量作为参数。这是一个仅仅为封装模块的而创建的函数,以便于在防止与Node.js的环境产生冲突。
(function (exports, require, module, __filename, __dirname) { // YOUR CODE INJECTED HERE! });
该Module._compile方法是同步执行的,所以对Module._load的调用只能等到这段代码运行结束,并将module.exprts返回给用户。
结论
因此,我们已经了解了require的全部代码,并已经初步了解它是如何工作的。
如果你已经按照这一切的方式做了,那么你已经为最后的秘密做好准备:require(‘module’)。这是正确的,该模块系统本身可以通过模块系统被加载。盗梦空间。这可能听起来很奇怪,但它可以让用户空间同模块加载系统互动起来,并不需要钻研Node.js核心。受欢迎的模块都像这样被建立。[2]
如果您想了解更多,请自己查看module.js源代码。还有很多东西足够你头痛一段时间了。第一个可以告诉我什么是NODE_MODULE_CONTEXTS“以及它为什么被添加的人可以得到加分奖励 :)
[1] module._compile方法只用于运行JavaScript文件。 JSON文件需通过JSON.parse() 解析并返回
[2]然而,这两个模块都建立在私有模块的方法,如Module._resolveLookupPaths和Module._findPath。你可以认为这并没有好多了…
The above is the detailed content of Explore how the require() method works in Node.js. For more information, please follow other related articles on the PHP Chinese website!