Detailed description of the use of require in node. js, node. jsrequire
The descriptions in the code comments are very clear, so there is not much nonsense here. The code is provided directly:
Copy codeThe 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 ");
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 ");
Var two = require ("./2.js ");
// 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 ");
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 ");
Var two1 = require ("./2.js ");
Console. log ("before deletion ")
Delete require. cache [require. resolve ("./2.js")];
Console. log ("deleted ");
Var two2 = require ("./2.js ");
/*
* Output result:
* Dr. Si
* Before Deletion
* After Deletion
* Dr. Si
**/
Do you know how to use require in node. js? If you have any questions, please leave a message.