Home > Web Front-end > Front-end Q&A > what is nodejs module

what is nodejs module

青灯夜游
Release: 2021-10-29 15:23:03
Original
1905 people have browsed it

Modules are the basic components of Node.js applications. Files and modules are in one-to-one correspondence. A Nodejs module is a file, and this file may be JavaScript code, JSON or compiled "C/ C" extension, the reference module can be used with the "require('file path')" statement.

what is nodejs module

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.

In order to allow Node.js files to call each other, Node.js provides a simple module system.

Modules are the basic components of Node.js applications, and files and modules have a one-to-one correspondence. In other words, a Node.js file is a module, which may be JavaScript code, JSON, or a compiled C/C extension.

For nodejs, a file is a module. You can export the interface or require other modules to come in.

// module1.js
exports.func1 = function(){
        console.log('func1 from module1 called');
}
Copy after login

Module1 uses function func1 as the public access interface of the module through the exports object.

//module2.js
var in_module1 = require('./module1.js');
in_module1.func1();
exports.func2 = function(){
        console.log('func2 from module2 called');
}
Copy after login

module2 requires module1 in. At this time, in_module1 is equivalent to the exports object of module1. When using in_module1 to call func1, it is equivalent to calling func1 through the exports object of module1.

At the same time, module2’s own function func2 also serves as the module2 public interface through the module’s exports object.

// module3.js
var in_module2 = require('./module2.js');
in_module2.func2();
Copy after login

Similarly, module3 requires module2. At this time, in_module2 is equivalent to the exports object of module2.

The running results are as follows:

rlan@rlan-LA:~/nodejs/nodetest$ node module2.js
func1 from module1 called
rlan@rlan-LA:~/nodejs/nodetest$ node module3.js
func1 from module1 called
func2 from module2 called
Copy after login

nodejs importing a module not only gets the public interface of the module, but also references other statements in the file, such as:

module1 .js is changed to

// module2.js
console.log('this is in module2');
var in_module1 = require('./module1.js');
  
in_module1.func1();
  
exports.func2 = function(){
       console.log('func2 from module2 called');
}
Copy after login

module2 introduces the func1 function of module1 and executes the print statement in module1:

rlan@rlan-LA:~/nodejs/nodetest$ node module1.js
this is in module1
rlan@rlan-LA:~/nodejs/nodetest$ node module2.js
this is in module2         - module2 self
this is in module1         - require module1
func1 from module1 called  - module2 self
Copy after login

Now, module2 loads module1 and module3 loads module2, what will happen if module3 loads module1 again?

// module3.js
var in_module1 = require('./module1.js');
var in_module2 = require('./module2.js');

in_module1.func1();
in_module2.func2();
Copy after login

At this time, module3 first loads module1, then module2, and module2 itself loads the part of module1. The running result is

rlan@rlan-LA:~/nodejs/nodetest$ node module3.js
this is in module1        -  require module1
this is in module2        -  require module2
func1 from module1 called -  require module2
func1 from module1 called -  module3 self
func2 from module2 called -  module3 self
Copy after login

If the require order of module3 is adjusted Look:

// module3.js
var in_module2 = require('./module2.js');
var in_module1 = require('./module1.js');

in_module1.func1();
in_module2.func2();
Copy after login

The running result is:

rlan@rlan-LA:~/nodejs/nodetest$ node module3.js
this is in module2         - require module2
this is in module1         - require module2
func1 from module1 called  - require module2
func1 from module1 called  - module3 self
func2 from module2 called  - module3 self
Copy after login

It seems that nodejs uses some mechanism to ensure that the same module will not be loaded repeatedly in another module, so

this is in module1
Copy after login

This line only appears once, although it seems to be loaded twice in module3.js.

So, what happens if there is a loop loading? Now we let module1 require module2:

// module1.js
console.log('this is in module1');

var in_module2 = require('./module2.js');

exports.func1 = function(){
        console.log('func1 from module1 called');
}
Copy after login
// module2.js
console.log('this is in module2');
var in_module1 = require('./module1.js');

in_module1.func1();

exports.func2 = function(){
        console.log('func2 from module2 called');
}
Copy after login

The running results are as follows:

rlan@rlan-LA:~/nodejs/nodetest$ node module1.js
this is in module1
this is in module2
/home/rlan/nodejs/nodetest/module2.js:4
in_module1.func1();
           ^
TypeError: in_module1.func1 is not a function
    at Object.<anonymous> (/home/rlan/nodejs/nodetest/module2.js:4:12)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/rlan/nodejs/nodetest/module1.js:3:18)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
rlan@rlan-LA:~/nodejs/nodetest$ node module2.js
this is in module2
this is in module1
func1 from module1 called
Copy after login

nodejs seems to prevent the behavior of loading itself. When running module2, the behavior is the same as the result of module1 not loading module2. Same, no error reported. When running module1, when I went to module2 and ignored the require module1 statement, module2 called func1 of module1, and the program went wrong.

To sum up, nested require statements that repeatedly load modules (or load themselves) in nodejs cannot be executed correctly.

[Recommended learning: "nodejs tutorial"]

The above is the detailed content of what is nodejs module. 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