Node.js modules are divided into two categories, one is the native (core) module and the other is the file module. The native module is compiled into the
binary execution file when the node.js source code is compiled, and the loading speed is the fastest. Another type of file module is dynamically loaded, and the loading speed is slower than native modules. However, Node.js caches both the native module
and the file module, so there will be no repeated overhead when requiring the second time. Among them, the native modules are defined under the lib directory, while the
file modules are uncertain.
//1. Create the test module js file (I named it test.js here)
//2. Add the test method
function test(){
console.log('Test Success!');
}
//3. Expose this method to the node module
//exports.test( This is the public method name. When calling externally, use this method name)
exports.test = test;
//4. Test (introduce this module in another js file , and call the corresponding test function, the two js files are in the same directory)
const testModule = require('./test.js');
testModule.test();
The above is the detailed content of Detailed explanation of the use of node js custom modules. For more information, please follow other related articles on the PHP Chinese website!