Node. js uses the require () function to load modules

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.

/* 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!

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.