How node. js's require () works

Source: Internet
Author: User

Most people know what the require () function does in node. js, but how many people know how it works? We use it every day to load library packages and modules, but its internal behavior is mysterious.

We follow the core of the Node module system: Module.js, which contains a surprisingly magical feature that is responsible for loading compiling and caching each used file, let's uncover its mystery.

function Module (ID, parent) {   this.id = id;   this.exports = {};   

This module type can be found in Module.js, which plays two main roles: first, it provides a basic function for all node. JS modules to build an instance from its file being loaded, even when the file is running, which is why we can add some attributes to Module.exports and When they are returned.

The second thing of module is to deal with the loading mechanism of node module, the standard require function is based on Module.require abstraction, the latter is just a simple wrapper for module._load, the loading method handles the actual loading of each file. Look at the code that looks like this:

1. Check if the module is cached in Module._cache
2. If the cache is empty, create a new module instance.
3. Save to cache
5. If there is an error loading and parsing the file
Remove the bad module from the cache

The module._load is responsible for loading the new modules and managing the module cache, which allows each module to reduce the read frequency of the file, thereby improving performance, and sharing module instances allowing the state to be saved across applications like a single-instance module.

If a module does not exist in the cache, Module._load will read the file to create a new one that reads the contents of the file successfully and will call Module._compile

If you notice the sixth step above, you will see that the return is Module.exports, which is why you can use exports and module.exports when you define a public interface, because they are really model._load and require returned.

Here's a look at Module._compile:

1. Create the Require standard function required by the calling module
3. Wrapping the JS code into a function, this function provides our require
Modules, such as variables localized to the scope of the module
4. Return to this function

Here's where magic happens, first of all, a special standard require function will be created, this is the familiar require () function, when the function wraps itself Module.require, it also contains some helpful properties and methods that few people know about, such as:

    • Require (): Load an external module
    • Require.resolve (): solves the module name based on its absolute path
    • Require.main: the main module
    • Require.cache: All cache modules
    • Require.extensions: File-based extensions are available for compiling methods.

Once the require is ready, the entire source code will be wrapped into a new function that has require module and exports and other exposed variables as parameters, creating a new function scope for the module so that the rest of the node. JS environment is not contaminated.

YOUR CODE injected here! Your code is here.

Finally, the function that wraps the module will run, the entire Module._compile method is executed synchronously, so that the original Module._load method call will wait for the code to run before it is finished, returning module.exports to the user.

 

How node. js's require () works

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.