Node. js loads the module using the require of function
- 2020-03-30 04:22:17
- OfStack
Detailed instructions are written in the notes, here is not wordy, the partners see their own details, do not as the air.
/* in node In the , You can use require() Function to load the module .
* require The function takes an argument , Parameter values can have the filename of the module with the full path , It can also be the module name . When using node When a module is provided in , in require The function simply specifies the module name .
* */
//Create a page 2.js; The code is
var name=" Think think Dr. ";
exports.name=name;
//Create a page 1.js; The code is
var two=require("./2.js");
console.log(two.name);
//Output result: si si doctor < br / >
/*
* in node All script files are a module file , so 1.js Also a module file , Also because the file is passed in a command line window node The command is run directly , So in node The module file is defined as the main module of the application
* The following method can be used to detect whether the current module is the main module
* */
if(module===require.main){
console.log(" The current module is the main module ");
}
//Output result: when the current module main module
//2. Js code
var name=" Think think Dr. ";
console.log(name);
exports.name=name;
//1. Js code :
var two=require("./2.js");
var two=require("./2.js");
//
/*require.resolve(str)
* in node In the , You can use this function to query the file name of a module file with the full absolute path .
* */
var url=require.resolve("./2");
console.log(url);
//Output :E: nodegys2. Js
/*require.cache
* in node In the , This property represents the cache for all loaded modules .
* */
var two=require("./2.js");
var cache=require.cache;
console.log(cache);
/* The output :
* { '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: ' Think think Dr. ' },
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=" Think think Dr. ";
console.log(name);
//1. Js code
//When a module object cached in the cache is deleted using the delete keyword, the code in the module is rerun the next time the module is loaded
var two=require("./2.js");
var two1=require("./2.js");
console.log(" Delete the former ")
delete require.cache[require.resolve("./2.js")];
console.log(" After deleting ");
var two2=require("./2.js");
/*
* The output :
* Think think Dr.
* Delete the former
* After deleting
* Think think Dr.
* */
Children whether to understand the use of node require() function, this article is their own understanding of some, if there is an omission, please correct.