1/* in node, you can use the require () function to load modules. 2 * 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. 3 **/4 // create a page 2.js; the code is as follows: 5 var name = "Dr. Sisi"; 6 exports. name = Name; 7 // create a page 1.js; the code is as follows 8 var two = require (". /2.js"); 9 console. log (two. name); 10 // output result: Dr. Sisi 11 12/* 13 * All script files in node are a module file, so 1. JS is also a module file, and because the file is directly run through the node command in the command line window, therefore, the module File in node is defined as the main module of the application 14 * you can use the following method to check whether the current module is the main module 15 **/16 if (module = = require. main) {17 console. log ("Current module current module"); 18} 19 // output result: Current module current Module 20 21 // 2. JS Code 22 var name = "Dr. Sisi"; 23 console. log (name); 24 exports. name = Name; 25 26 // 1. JS Code: 27 var two = require (". /2.js"); 28 var two = require (". /2.js"); 29 // although it is referenced twice, it only executes the console once. log (name) output. 30 31/* require. resolve (STR) 32 * in node, you can use this function to query the file names with full absolute paths for a module File. 33 **/34 var url = require. resolve (". /2 "); 35 console. log (URL); 36 // output result: e: \ node \ gys \ 2. js37 38/* require. cache39 * in node, this attribute represents the cache areas of all loaded modules. 40 **/41 42 var two = require (". /2.js"); 43 var cache = require. cache; 44 console. log (cache); 45/* output result: 46 * {'e: \ node \ gys \ 1. js': 47 {ID :'. ', 48 exports :{}, 49 parent: NULL, 50 filename: 'e: \ node \ gys \ 1. js', 51 loaded: false, 52 children: [[object], 53 paths: 54 ['e: \ node \ gys \ node_modules ', 55'e: \ node \ node_modules ', 56 'e: \ node_modules']}, 57 'e: \ node \ gys \ 2. js': 58 {ID: 'e: \ node \ gys \ 2. js', 59 exports: {Name: 'siss'}, 60 parent: 61 {ID :'. ', 62 exports :{}, 63 Parent: NULL, 64 filename: 'e: \ node \ gys \ 1. js', 65 loaded: false, 66 children: [object], 67 paths: [object]}, 68 filename: 'e: \ node \ gys \ 2. js', 69 loaded: True, 70 children: [], 71 paths: 72 ['e: \ node \ gys \ node_modules ', 73 'e: \ node \ node_modules ', 74 'e: \ node_modules']} 75 **/76 77 78 // 2. JS Code 79 var name = "Dr. Sisi"; 80 console. log (name); 81 // 1. JS Code 82 // when the delete keyword is used to delete a module object cached in the cache, the code in the module will be re-run the next time the module is loaded. code: 83 84 var two = require (". /2.js"); 85 var two1 = require (". /2.js"); 86 console. log ("before deletion") 87 Delete require. cache [require. resolve (". /2.js")]; 88 console. log ("deleted"); 89 var two2 = require (". /2.js"); 90/* 91 * output result: 92 * Dr. Si 93 * before deletion 94 * after deletion 95 * Dr. Si 96 **/
Require in node