Node. js uses the require () function to load module _ node. js

Source: Internet
Author: User
This article uses several examples to analyze how to use the require () function in node. js to load modules. It is very detailed. For more information, please refer to the detailed descriptions below and write them in comments. Here we will not be so embarrassed. Let's take a look at the details here. Please never comment it out as air.

The Code is as follows:


/* In node, you can use the require () function to load modules.
* The require function uses a parameter. The parameter value can contain the name of the module in the complete path or the module name. when using the module provided by node, you only need to specify the module name in the require function.
**/
// Create a page 2.js; the code is as follows:
Var name = "Dr. Sisi ";
Exports. name = name;
// Create a page. The Code is as follows:
Var two = require ("./2.js? 6.1.3 ");
Console. log (two. name );
// Output result: Dr. Si
/*
* All the script files in node are a module file, so 1. js is also a module File. Because the file is directly run through the node command in the command line window, the module File in node is defined as the main module of the application.
* You can use the following method to check whether the current module is the main module.
**/
If (module === require. main ){
Console. log ("Main module at current module ");
}
// Output result: the current module is the main module.
// 2. js Code
Var name = "Dr. Sisi ";
Console. log (name );
Exports. name = name;
// 1. js code:
Var two = require ("./2.js? 6.1.3 ");
Var two = require ("./2.js? 6.1.3 ");
// Although it is referenced twice, only the output of console. log (name) is executed once.
/* Require. resolve (str)
* In node, you can use this function to query the file names with full absolute paths for a module File.
**/
Var url = require. resolve ("./2 ");
Console. log (url );
// Output result: E: \ node \ gys \ 2.js
/* Require. cache
* In node, this attribute represents the cache areas of all loaded modules.
**/
Var two = require ("./2.js? 6.1.3 ");
Var cache = require. cache;
Console. log (cache );
/* Output result:
* {'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. ss '},
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 the delete keyword is used to delete a module object cached in the cache area, the code in the module will be re-run the next time the module is loaded. Use the Code:
Var two = require ("./2.js? 6.1.3 ");
Var two1 = require ("./2.js? 6.1.3 ");
Console. log ("before deletion ")
Delete require. cache [require. resolve ("./2.js? 6.1.3 ")];
Console. log ("deleted ");
Var two2 = require ("./2.js? 6.1.3 ");
/*
* Output result:
* Dr. Si
* Before Deletion
* After Deletion
* Dr. Si
**/

Do children's shoes know the use of the require () function in node? This article is my own understanding. If you have any omissions, please correct them.

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.