Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of node js custom modules

零下一度
Release: 2017-06-27 14:39:30
Original
1238 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template