How to Use require in node. js _ node. js-js tutorial

Source: Internet
Author: User
This article analyzes and explains node through several examples. the use of require in js is very detailed. Here we recommend that you provide a very clear description in the code comments. Here we will not talk nonsense about it. We will provide the Code directly:

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? 1.1.23 ");
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? 1.1.23 ");
Var two = require ("./2.js? 1.1.23 ");
// 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? 1.1.23 ");
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? 1.1.23 ");
Var two1 = require ("./2.js? 1.1.23 ");
Console. log ("before deletion ")
Delete require. cache [require. resolve ("./2.js? 1.1.23 ")];
Console. log ("deleted ");
Var two2 = require ("./2.js? 1.1.23 ");
/*
* 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.

Related Article

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.