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.
/* 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 1.js; the code is as follows var two = require (". /2.js"); console. log (two. name); // output result: 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 * you can use the following method to check whether the current module is the main module **/if (module = require. main) {console. log ("Main module in current module");} // output: Main module in 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 is referenced twice, it only executes the console once. log (name) output. /* 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: 'siss'}, 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, the code in the module will be re-run the next time the module is loaded. 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 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.
For more articles about using the require () function to load modules in node. js, refer to PHP!