Node.js use the Require () function to load modules _node.js

Source: Internet
Author: User

Detailed instructions are written in the comments, here is not long-winded, small partners to see their own detailed, do not comment as air.

Copy Code code as follows:

/* In node, you can use the Require () function to load the module.
* The Require function uses a parameter that can have the file name of a module with a full path, or a module name. When you use the module provided in node, you only need to specify the module name in the Require function.
* */
Create a page of 2.js code as follows
var name= "Dr. Sisi";
Exports.name=name;
Create a page of 1.js code as follows
var two=require ("./2.js");
Console.log (Two.name);
Output Result: Dr. Sisi
/*
* All script files in node are a module file, so the 1.js is also a module file, and because the file is run directly through the node command in the Command Line window, the module file is defined as the main module of the application in node
* You can detect whether the current module is the main module in the following ways
* */
if (Module===require.main) {
Console.log ("Current Module time main module");
}
Output results: The main module is the current module
2.js Code
var name= "Dr. Sisi";
Console.log (name);
Exports.name=name;
1.js Code:
var two=require ("./2.js");
var two=require ("./2.js");
Although it was referenced 2 times, only 1 Console.log (name) outputs were executed.
/*require.resolve (str)
* In node, you can use this function to query a filename with a full absolute path to a module file.
* */
var url=require.resolve ("./2");
Console.log (URL);
Output Result: E:\node\gys\2.js
/*require.cache
* In node, this property represents the buffer for all loaded modules.
* */
var two=require ("./2.js");
var Cache=require.cache;
Console.log (cache);
/* Output results:
* {' e:\\node\\gys\\1.js ':
{ID: '. ',
Exports: {},
Parent:null,
FileName: ' E:\\node\\gys\\1.js ',
Loaded:false,
Children: [[Object]],
Paths
[' E:\\node\\gys\\node_modules ',
' E:\\node\\node_modules ',
' E:\\node_modules ']},
' E:\\node\\gys\\2.js ':
{ID: ' e:\\node\\gys\\2.js ',
Exports: {name: ' Dr. Sisi '},
Parent
{ID: '. ',
Exports: {},
Parent:null,
FileName: ' E:\\node\\gys\\1.js ',
Loaded:false,
Children: [Object],
Paths: [Object]},
FileName: ' E:\\node\\gys\\2.js ',
Loaded:true,
Children: [],
Paths
[' E:\\node\\gys\\node_modules ',
' E:\\node\\node_modules ',
' E:\\node_modules ']}}
* */
2.js Code
var name= "Dr. Sisi";
Console.log (name);
1.js Code
When you use the Delete keyword to delete a cached module object in the cache, the code in that module is rerun the next time the module is loaded. Use code:
var two=require ("./2.js");
var two1=require ("./2.js");
Console.log (before "delete")
Delete Require.cache[require.resolve ("./2.js")];
Console.log ("after deletion");
var two2=require ("./2.js");
/*
* Output Result:
* Dr. Sisi
* Before deleting
* After deletion
* Dr. Sisi
* */

Do children's shoes understand the use of require () function in node, this article is some of their own understanding, if there are omissions, but also please correct me.

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.